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

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

數組對象到對象數組

數組對象到對象數組

一只名叫tom的貓 2023-06-15 16:50:58
我想更改作為嵌套數組對象的輸入: const bullets = {  one: [  [    1053, 2  ],  [    2222, 7  ]],  two: [   [    1053, 4   ],   [    2222, 1   ]],  three: [   [    1053, 6   ],   [    2222, 12   ],],};到映射的輸出中,它應該是這樣的:const result = [  {label: 1053, a: 2, b: 4, c: 6},  {label: 2222, a: 7, b: 1, c: 12}]所以基本上將輸入對象數組中的值映射到結果中的鍵。對象的數量對應于唯一標簽的數量(每個子數組中的“第一個”值)。每個子數組中的“second”值將“a”、“b”和“c”值分配給具有相應標簽的對象。我試過這樣的事情:const bullets = {  one: [    [      1053, 2    ],    [      2222, 7    ]  ],  two: [    [      1053, 4    ],    [      2222, 1    ]  ],  three: [    [      1053, 6    ],    [      2222, 12    ],  ],};let result = [];Object.keys(bullets).reduce((p, c) => {  result.push(bullets[p]    .map(v => ({      label: v[0],      a: v[1],      b: bullets[c].find(value => value[0] === v[0])[1],      c: bullets[c].find(value => value[0] === v[0])[1],    })));  return c;});console.log([].concat(...result));它適用于輸入對象只有兩個條目的情況,我只需要輸出中的“a”和“b”值,但這顯然是錯誤的,因為它導致 4 個對象而不是兩個(“先前”值在reduce 曾經是第一個條目,在另一次迭代中是第二個條目 - 類似于 reduce 中的“當前”值)。你知道這是否可以通過更簡單的方式實現嗎?也許某種 lodash 解決方案?我得到的一切都是意大利面條,沒有確切的結果。提前感謝您的提示。
查看完整描述

4 回答

?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

如果已經存在具有當前標簽的對象,您可以減少對象值并檢查每次迭代。如果存在,則將值添加到它的數組中。

現在,與其擁有 , 之類的屬性a,b不如c我建議只使用數組并將它們解構為變量。這樣您就不會受限于字母表或必須事先定義每個屬性。

const bullets = {

? one:? ?[ [ 1053, 2 ], [ 2222,? 7 ] ],

? two:? ?[ [ 1053, 4 ], [ 2222,? 1 ] ],

? three: [ [ 1053, 6 ], [ 2222, 12 ] ]

};


const result = Object.values(bullets).reduce((acc, items) => {

? for(const [key, value] of items) {

? ? const index = acc.findIndex(({label}) => label === key);

? ??

? ? index >= 0

? ? ? ? acc[index].values.push(value)

? ? ? : acc.push({label: key, values: [value]})

? }

??

? return acc;

}, []);


// All objects

console.log(result);


// a, b, c

for(const item of result) {

? const [a, b, c] = item.values;

? console.log(a, b, c);

}

.as-console-wrapper { max-height: 100% !important; }


查看完整回答
反對 回復 2023-06-15
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

我將創建一個要使用的屬性數組(例如a、b和c),然后創建一個由標簽索引的對象,其值是數組的項目result。然后您可以遍歷輸入并根據需要插入值:


 const bullets = {

  one: [

  [

    1053, 2

  ],

  [

    2222, 7

  ]

],

  two: [

   [

    1053, 4

   ],

   [

    2222, 1

   ]

],

  three: [

   [

    1053, 6

   ],

   [

    2222, 12

   ],

],

};

const properties = ['a', 'b', 'c'];


const result = {};

Object.values(bullets).forEach((arr, propIndex) => {

  arr.forEach(([label, val]) => {

    if (!result[label]) result[label] = { label };

    result[label][properties[propIndex]] = val;

  });

});

console.log(Object.values(result));

  {label: 1053, a: 2, b: 4, c: 6},

  {label: 2222, a: 7, b: 1, c: 12}

]


查看完整回答
反對 回復 2023-06-15
?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

我的回答更像是 C 風格,但它做同樣的工作。


const bullets = {

  one: [

  [

    1053, 2

  ],

  [

    2222, 7

  ]

],

  two: [

   [

    1053, 4

   ],

   [

    2222, 1

   ]

],

  three: [

   [

    1053, 6

   ],

   [

    2222, 12

   ],

],

};


var midResult = {}

var results = []

for (const key in bullets) {

const arr = bullets[key];

arr.forEach(elem => {

    if (midResult.hasOwnProperty(elem[0])) {

        midResult[elem[0]].push(elem[1])

    }else{

        midResult[elem[0]]  =[elem[1]]

    }

});

}

for (const key in midResult) {

if (midResult.hasOwnProperty(key)) {

    const arr = midResult[key];

    var elem = {}

    elem.label = key

    arr.forEach( (e,i) =>{

        elem[String.fromCharCode(97+i)] = e

    })

    results.push(elem)

}

}

console.log(results)


查看完整回答
反對 回復 2023-06-15
?
達令說

TA貢獻1821條經驗 獲得超6個贊

您暗示鍵名 (?'a',?'b',?'c') 并不重要。這是一種重用原始名稱 (?'one',?'two',?'three') 的技術。

const transform = (bullets) => Object .entries (Object .entries (bullets)

? .reduce ((a, [name, xs]) =>?

? ? xs .reduce (

? ? ? (a, [label, value]) => ({...a, [label]: [...(a [label] || []), [name, value]]}),

? ? ? a

? ? ), {}

? )

) .map (([label, values]) => ({label: Number(label), ...Object .fromEntries (values)}))



const bullets = {one: [[1053, 2], [2222, 7]], two: [[1053, 4], [2222, 1]], three: [[1053, 6], [2222, 12]]}


console .log (

? transform (bullets)

)

.as-console-wrapper {max-height: 100% !important; top: 0}


我們分幾步進行。調用Object .entries (bullets)產生這個:


[

? ["one", [[1053, 2], [2222, 7]]],?

? ["two", [[1053, 4], [2222, 1]]],?

? ["three", [[1053, 6], [2222, 12]]]

]

然后雙重歸約把它變成這樣:


{

? "1053": [["one", 2], ["two", 4], ["three", 6]],?

? "2222": [["one", 7], ["two", 1], ["three", 12]]

}

然后我們Object .entries再次調用這個結果得到


[

? ["1053", [["one", 2], ["two", 4], ["three", 6]]],?

? ["2222", [["one", 7], ["two", 1], ["three", 12]]]

]

最后,通過map調用 using Object.fromEntries,我們把它變成


[

? {label: 1053, one: 2, two: 4, three: 6,?

? {label: 2222, one: 7, two: 1, three: 12}

]

如果您確實需要密鑰轉換,我們可以在調用中提供它們,如下所示:


transform (bullets, {one: 'a', two: 'b', three: 'c'})

只需對代碼稍作修改。


const transform = (bullets, keys) => Object .entries (Object .entries (bullets)

? .reduce ((a, [name, xs]) =>?

? ? xs .reduce (

? ? ? (a, [label, value]) => ({...a, [label]: [...(a [label] || []), [keys[name] || name, value]]}),

? ? ? a

? ? ), {}

? )

) .map (([label, values]) => ({label: Number(label), ...Object .fromEntries (values)}))



const bullets = {one: [[1053, 2], [2222, 7]], two: [[1053, 4], [2222, 1]], three: [[1053, 6], [2222, 12]]}


console .log (

? transform (bullets, {one: 'a', two: 'b', three: 'c'})

)

.as-console-wrapper {max-height: 100% !important; top: 0}


查看完整回答
反對 回復 2023-06-15
  • 4 回答
  • 0 關注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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