简体   繁体   English

如何将键值对添加到 javascript 中的关联数组

[英]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:所以你的目标可以分成更小的部分:

  • just create a key in object只需在 object 中创建一个密钥
  • assign an array to the above key将数组分配给上述键
  • push desired values into array将所需值推入数组

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.

相关问题 如何在javascript中的顺序中添加key:value对 - How do I add key:value pair in an order in javascript 如何在jquery / javascript中的键/值数组中的特定点插入键值对? - How do I insert a key value pair at a specific point in a key/value array in jquery/javascript? 如何将键值对添加到具有多个值的 javascript 对象数组中? - How to add a key value pair to an array of objects in javascript with multiple value? 如何在Javascript / Jquery中向数组添加键值对 - How to add a key value pair to an array in Javascript / Jquery 如何在javascript中向数组添加新对象(键值对)? - How to add a new object (key-value pair) to an array in javascript? 将键值对添加到 javascript 中的对象数组? - Add a Key Value Pair to an array of objects in javascript? 如何在现有的JavaScript对象中添加新的key:value对? - How do I add a new key:value pair in existing JavaScript object? 如何在 javascript 中做得更好:在迭代时将一对(键,值)添加到 hash? - how can I do this better in javascript: add a pair (key, value) to a hash while iterating? 如何将键/值对添加到 JavaScript 对象? - How can I add a key/value pair to a JavaScript object? 如何从 JavaScript 对象数组中获取键值对? - How do I get key-value pair from array of JavaScript objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM