简体   繁体   English

用多个键和值填充对象数组

[英]Fill array of objects with multiple keys and values

I have a method on @change which recieve these values.我有一个关于@change 的方法,它接收这些值。

 changeAttr(id, key, value) {
  const selections = [];
},

id could be any number, key could be: color, size, sex, etc..., value could be: red, 8, female, etc. In the first reception the values can be for example: id = 3 , key = "color" , value = "red" and they change when the user select another option. id可以是任意数字, key可以是:color、size、sex 等, value可以是:red、8、female 等。在第一次接收中,值可以是: id = 3 , key = "color" , value = "red"并且当用户选择另一个选项时它们会改变。 For example: id = 3 , key = "sex" , value = "female" or id = 5 , key = "size" , value = "50" ...etc例如: id = 3 , key = "sex" , value = "female"id = 5 , key = "size" , value = "50" ...等

I want to dynamically fill an array of objects with the values this way for example.例如,我想用这种方式动态填充对象数组。

selections = [{
              "3": { 
                  "color": "red",
                  "sex": "male",
                  "size": "40" 
              },
              {
              "5": { 
                  "color": "black",
                  "sex": "female",
                  "size": "36" 
              },
              {
              "8":{ 
                 "color": "black",
                 "sex": "female",
                 "size": "36" 
              ...
              },
              ...
              }];

I want to overwrite the values ​​if the key already exists for the same id.如果相同 id 的键已经存在,我想覆盖这些值。 If it does not exist, it must be added for its id.如果它不存在,则必须为其添加 id。

I hope I have explained clearly.我希望我已经解释清楚了。 Thank you very much for your time非常感谢您的宝贵时间

You can simply use an array syntax :您可以简单地使用数组语法:

Considering考虑

let yourObject = {}

You can use [] to define a property您可以使用 [] 来定义属性

yourObject["color"] = "red"

So with your logic, you can do :因此,根据您的逻辑,您可以执行以下操作:

yourObject[key] = value

Tip : Using int strings as index is not really a good pratice because JS reindexes arrays, i advise you to construct your object like this :提示:使用 int 字符串作为索引并不是一个好的实践,因为 JS 重新索引数组,我建议你像这样构造你的对象:

 [ { id: 3 color: "red", sex: "male", size: "40" }, { id: 5, color: "black", sex: "female", size: "36" }, { id: 8, color: "black", sex: "female", size: "36" }, ... ];

EDIT :编辑 :

 const selections = []; function changeAttr(id, key, value) { // Get the index in selection by id let index = selections.map(t=>t.id).indexOf(id) if (index !== - 1) { // if the id have been found selections[index][key] = value // It create the index "key" if not found, and replace the value if found } else { // if the id have not been found let tmp = { id: id } tmp[key] = value selections.push(tmp) } } console.log(selections) changeAttr(6, "color", "red") console.log(selections) changeAttr(3, "sex", "female") console.log(selections) changeAttr(6, "sex", "male") console.log(selections) changeAttr(6, "color", "yellow") console.log(selections)

You can run snippet to see, I think that is what you are looking for你可以运行snippet来查看,我想这就是你要找的

newValue =  { 
  "color": "black",
  "sex": "female",
  "size": "36" 
 }


selections.forEach((oItem)=>{
  for(let key in oItem){
    if(oItem.hasOwnProterty(key)){
       // overwrite
    }else {
    // set the newValue 
    }

  }
  
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM