简体   繁体   English

如何在 javascript 中的另一个数组 object 中移动特定键值

[英]How to move specific key value inside another array object in javascript

I have array object obj how to move particular key value store inside another array items in javascript.我有数组 object obj如何将特定键值store移动到 javascript 中的另一个数组items中。 I would like to know how to pass particular key value inside another array in javascript我想知道如何在 javascript 的另一个数组中传递特定的键值

var obj = [
  {
   id:1,
   store: "10",
   items: [{name:"sample1", total: 20}, {name:"sample2", total: 10}] // add store
  },
  {
   id:1,
   store: "11",
   items: [{name:"sample3", total: 10}, {name:"sample4", total: 10}] // add store
  }
]

function newarray(obj){
 return obj.map(e=>...e,e.items.map(i=>{...i,store: e.store })
}

Expected Output预计 Output

[
  {
   id:1,
   store: "10",
   items: [{name:"sample1", total: 20, store: "10"}, {name:"sample2", total: 10, store: "10"}]
  },
  {
   id:1,
   store: "11",
   items: [{name:"sample3", total: 10, store: "11"}, {name:"sample4", total: 10, store: "11"}] 
  }
]

You are almost in the right direction, just few changes need to be done in your map function to get expected output:您几乎朝着正确的方向前进,只需在map function 中进行一些更改即可获得预期的 output:

 var obj = [ { id:1, store: "10", items: [{name:"sample1", total: 20}, {name:"sample2", total: 10}] }, { id:1, store: "11", items: [{name:"sample3", total: 10}, {name:"sample4", total: 10}] }]; var result = obj.map(val=>(val.items = val.items.map(p=>({...p,store:val.store})), val)); console.log(result);

You can use reduce and forech if you want to try a different approach如果您想尝试不同的方法,可以使用reduceforech

 var obj = [ { id:1, store: "10", items: [{name:"sample1", total: 20}, {name:"sample2", total: 10}] }, { id:1, store: "11", items: [{name:"sample3", total: 10}, {name:"sample4", total: 10}] } ] newob=obj.reduce((acc,curr)=>{ curr.items.forEach(x=>{ x.store=curr.store }) return [...acc,{...curr}] },[]) console.log(newob)

暂无
暂无

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

相关问题 如何从JavaScript中的数组对象中删除特定的键和值? - How to remove specific key and value from array object in javascript? 如何检查数组中 object 的键是否在 javascript 中具有特定值 - How to check if key of object in array has specific value in javascript 如何在对象数组中分离每个对象的键和值-Javascript - How to separate key and value of each object inside array of objects - Javascript 在Javascript中,如何检查嵌套在另一个键/值对中的特定键/值对的存在? - In Javascript, how can I check the existence of a specific key/value pair nested inside another key/value pair? 将 object 数组中的键值替换为另一个具有相同键的 object - Replace key value inside array of object with another object with same key 在嵌套在另一个数组中的数组中返回具有特定key:value对的对象-Javascript - Return object with specific key:value pair in an array nested in another array - Javascript 在Javascript对象中移动键/值 - Move key/value in Javascript Object 如何在JavaScript中使用特定键将数组转换为对象 - how convert array to object with specific key in javascript 如何将 object 移动到同一数组中的另一个 object 中 - How to move object inside another object in the same array 如何从所有对象中删除一个简单的特定键值对并添加到数组内的一个特定 object - How to remove a simple specific key value pair from all objects and add to one specific object inside an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM