简体   繁体   English

如何动态传递数组值作为对象道具?

[英]How to pass array values as an object prop dynamicly?

I need to treat array values as props of object.我需要将数组值视为对象的道具。 For example:例如:

let arr = ['masa_icerik', 'urunler', 0, 'urun_adet'];
let obj = {
  "_id": "5c13bd566704aa5e372dddcf",
  "masa_id": 3,
  "masa_numara": 3,
  "masa_magaza": 1,
  "masa_icon": "kola",
  "masa_adi": "salon 3",
  "masa_durum": 1,
  "masa_icerik": {
    "adisyon": "J1554745811908",
    "urunler": [{
      "urun_adet": 14,
      "urun_fiyat": 3,
      "urun_id": "5c16686b93d7b79ae6367864",
      "urun_odenen": 0
    }, {
      "urun_adet": 1,
      "urun_fiyat": 5,
      "urun_id": "5c16686b93d7b79ae6367865",
      "urun_odenen": 0
    }]
  },
  "masa_acilis": "2019-04-08T17:50:12.052Z",
  "masa_acan": "5c1eda01d1f4773110dd6ada"
};

I have an array and an object like above and I want to do something like this:我有一个数组和一个像上面这样的对象,我想做这样的事情:

let res;
arr.forEach(elem => {
   res = obj[elem];
});

and after that I need to get something like :之后我需要得到类似的东西:

obj['masa_icerik']['urunler'][0]['urun_adet']

The number of the values is dynamic from server.值的数量是来自服务器的动态值。 Thats why i need something like this.这就是为什么我需要这样的东西。 Is there any way to do that?有没有办法做到这一点? I need to change that property and return the changed obj.我需要更改该属性并返回更改后的 obj。

You can use forEach loop to loop thru the array and store it to a temp variable.您可以使用forEach循环遍历数组并将其存储到临时变量中。 If all elements exist, it will change the value.如果所有元素都存在,它将改变值。

 let arr = ['a', 'b', 'c']; let obj = {'a':{'b':{'c':1}}}; let newValue = "NEW VALUE"; let temp = obj; arr.forEach((o, i) => { if (i < arr.length - 1) temp = temp[o] || null; else if (temp !== null && typeof temp === "object" ) temp[o] = newValue; }); console.log(obj);


If there are multiple multiple object properties missing in the last part of the array.如果数组的最后部分缺少多个多个对象属性。

 let arr = ['a', 'b', 'c', 'd']; let obj = {'a': {'b': {}}}; let newValue = "NEW VALUE"; let temp = obj; arr.forEach((o, i) => { if (i < arr.length - 1) { if (!temp[o]) temp[o] = {[arr[i + 1]]: {}}; temp = temp[o]; } else if (temp !== null && typeof temp === "object") temp[o] = newValue; }); console.log(obj);

You can use references您可以使用references

Here idea is这里的想法是

  • Initialize val with object reference使用object引用初始化val
  • Loop through array and keep setting new reference to val循环遍历数组并继续设置对val新引用

 let arr = ['a','b','c']; let obj = {'a':{'b':{'c':1}}}; let getMeValue = (arr) => { let val=obj; arr.forEach(e => val = val && val[e] ) return val } console.log(getMeValue(arr)) console.log(getMeValue([1,2,3]))

UPDATE: I want to change values更新:我想更改值

 let arr = ['a','b','c']; let obj = {'a':{'b':{'c':1}}}; let getMeValue = (arr) => { let val = obj arr.forEach((e,i) => { if(i === arr.length-1 && val){ val[e] = 5 } else { val = val && val[e] } }) return obj } console.log(getMeValue(arr))

I am not fully understanding where you are getting the new values from but I think this will get you on the right track.我不完全理解您从哪里获得新值,但我认为这将使您走上正确的轨道。

let newObj = {};

arr.map(each => {
   newObj[each] = "new value";
 })

console.log(newObj);

I'm not sure about your requirment here, I guess you want the below:我不确定您在这里的要求,我想您需要以下内容:

 let func = (arr, value)=>{ r = {}; r[arr[arr.length-1]] = value; for(let i = arr.length-2; i>=0; i--){ obj = {}; obj[arr[i]] = r; r = obj; } return r; } console.log(func(['a', 'b', 'c'], 1));

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

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