亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取每個對象鍵的 id

如何獲取每個對象鍵的 id

胡說叔叔 2022-07-08 16:16:52
l 有以下數據json,包含在對象鍵上,這些鍵是定期更改的  "2461ba4c": [ // <----------- get the main key for each one     "4BD436",    36.346,    33.478,  ],  "2461b87c": [    "06A128",    34.628,    33.584,  ]我想為每個數組獲取對象的鍵。我確實過濾了鍵.filter(key => key ),但它給了我對象的所有鍵。我想給每個數組主鍵對象。function data() {  $.ajax('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', {    type: 'GET',    dataType: 'jsonp',    timeout: 5000  }).done(function(data, textStatus, jqXHR) {    Object.keys(data)      .map(key => data[key])      .map((position) => ({        lat: position[1],        lng: position[2],                        // <----------- Add the main key here for each array      })).filter(position => position.lat && position.lng).forEach(i => {        console.log(i.lat, i.lng, i.heading)      })  })}預期輸出:2461b87c, // <---main key34.628,33.584,
查看完整描述

2 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

您可以在 map 方法中創建一個新的 obj 并分配鍵值,例如,


const dataObj = {

    lat: position[1],

    lng: position[2],

    heading: key

  }

并返回對象。


片段如下,


const data = {

  "2461ba4c": [

    "4BD436",

    36.346,

    33.478,

  ],

  "2461b87c": [

    "06A128",

    34.628,

    33.584,


  ]

}


const newData = Object.keys(data).map(key => {

  const position = data[key];

  const dataObj = {

    lat: position[1],

    lng: position[2],

    heading: key

  }

  return dataObj;

})


newData.filter(position => position.lat && position.lng).forEach(i => {

    console.log(i.lat, i.lng, i.heading)

})


查看完整回答
反對 回復 2022-07-08
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

我們可以in為此使用循環


let arr =  {"2461ba4c": [ 

    "4BD436",

    36.346,

    33.478,

  ],

  "2461b87c": [

    "06A128",

    34.628,

    33.584,

  ]};

  

for( let a in arr){

  document.write(a+"<br>"); 

}


查看完整回答
反對 回復 2022-07-08
  • 2 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號