[英]How do i add a key value pair to an asociative array in javascript
let finaloutput = {};
$.each( arr, function(index,key) {
let outarray = [];
// var id sourced from another function
outarray.push(id,index,key);
//??finaloutput[id].push(outarray);
}
In the above code i am trying to store an object similar to below.在上面的代码中,我试图存储类似于下面的 object。 Each time the loop grabs the same id it appends the array in the finaloutput object eg id:index:key
每次循环获取相同的 id 时,它会将数组附加到最终输出 object 例如 id:index:key
1st loop where id = 7 index=param 1 key=val1第一个循环,其中 id = 7 index=param 1 key=val1
{"7":[{param1:val1}]}
2nd.. id = 7 index=param 2 key=val2第二个.. id = 7 index=param 2 key=val2
{"7":[{param1:val1,param2:val2}]}
3rd... id = 8 index=param 1 key=val1第三... id = 8 index=param 1 key=val1
{"7":[{param1:val1,param2:val2}],"8":[{param1:val1}]}
How do i achieve this我如何实现这一目标
I tried to generated similar output using sample data:我尝试使用示例数据生成类似的 output :
`let indx = ["param1", "param2", "param3", "param4", "param5"]
let key = ["k1", "k2", "k3", "k4", "k5"]
let id = ["1", "2", "3", "4", "5"]
let resultObj = {}
for (let i = 0; i < 5; i++) {
if (resultObj[id]) {
let temp=Object.assign({}, {[indx[i]]:key[i]}, ...resultObj[id[i]])
resultObj[id[i]] = [temp];
}
else{
let ob=[{[indx[i]]:key[i]}]
resultObj[id[i]]=ob
}
}
console.log(resultObj)`
In your case you can do something like: let finaloutput = {}; $.each(arr, function (index, key) { if (finaloutput[id]) { let temp = Object.assign({}, { [index]: key }, ...finaloutput[id]) finaloutput[id] = [temp]; } else { let temp2 = [{ [index]: key }] finaloutput[id] = temp2 } }
在您的情况下,您可以执行以下操作:
let finaloutput = {}; $.each(arr, function (index, key) { if (finaloutput[id]) { let temp = Object.assign({}, { [index]: key }, ...finaloutput[id]) finaloutput[id] = [temp]; } else { let temp2 = [{ [index]: key }] finaloutput[id] = temp2 } }
let finaloutput = {}; $.each(arr, function (index, key) { if (finaloutput[id]) { let temp = Object.assign({}, { [index]: key }, ...finaloutput[id]) finaloutput[id] = [temp]; } else { let temp2 = [{ [index]: key }] finaloutput[id] = temp2 } }
Note Please refer to my example to get better understanding incase I was not able to exactly formulate answer of your code or it gives error注意请参考我的示例以获得更好的理解,以防我无法准确地制定代码的答案或它给出错误
You have an object finaloutput
.你有一个 object
finaloutput
。 So your goal can be divided into smaller pieces:所以你的目标可以分成更小的部分:
So the code should look like this:所以代码应该是这样的:
let finaloutput = {};
$.each( arr, function(index,key) {
finaloutput[key] = [];
let outarray = [];
// var id sourced from another function
finaloutput[key].push({id:key});
}
Or it can be done using reduce()
method.或者可以使用
reduce()
方法来完成。 Let me show an example::让我举个例子::
const array = ["one", "two", "three", "one", "one", "two", "two", "three", "four", "five"]; const result = array.reduce((a, c) => { a[c]=a[c] || []; a[c].push({yourKey: c}); return a; }, {}) console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.