我正在遍历一个数组数组.map(),试图将内部数组作为一key:value对分配给一个对象。它不成功。这甚至可以工作吗?我需要把它分解成更多的步骤/功能/变量吗?
对于属性分配中的元素,我已经尝试了我能想到的所有语法。
我还尝试将arr.map(function() {})语法与所有这些一起使用,但没有区别。
const smallArr = [1, 23, 25, 46, 52, 789, 64];
const thisArr = [5, 4, 65, 24, 35];
const thatArr = [{key: 1, anotherKey: 2}];
let parentObj = {};
const bigArr = [smallArr, thisArr, thatArr];
const newSomething = bigArr.map(arr => parentObj["arr"] = arr);
// parentObj returns: { arr: [ { key: 1, anotherKey: 2 } ] }
这两个我都懂。它在每次迭代时分配我的元素的字符串并覆盖值,以便最终对象是一个键:值对,值是外部数组中的最后一个内部数组。
const newSomething = bigArr.map(arr => parentObj.arr = arr);
//returns { arr: [ { key: 1, anotherKey: 2 } ] }
const newSomething = bigArr.map(arr => parentObj['arr'] = arr);
//returns { arr: [ { key: 1, anotherKey: 2 } ] }
这个我不明白最后一对发生了什么。
const newSomething = bigArr.map(arr => parentObj[`${arr}`] = arr);
// returns {
'1,23,25,46,52,789,64': [ 1, 23, 25, 46, 52, 789, 64 ],
'5,4,65,24,35': [ 5, 4, 65, 24, 35 ],
'[object Object]': [ { key: 1, anotherKey: 2 } ],
arr: [ { key: 1, anotherKey: 2 } ]
}
这个我完全看不懂。
const newSomething = bigArr.map(arr => parentObj["`${arr}`"] = arr);
// returns {
'`${arr}`': [ { key: 1, anotherKey: 2 } ],
arr: [ { key: 1, anotherKey: 2 } ]
}
我正在尝试:
parentObj = {
smallArr: [1, 23, 25, 46, 52, 789, 64],
thisArr: [5, 4, 65, 24, 35],
thatArr: [{key: 1, anotherKey: 2}],
}
一只甜甜圈
相关分类