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; }

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}
]

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)

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}
添加回答
舉報