简体   繁体   English

JAVASCRIPT - 从对象的父对象数组中获取值,在数组展平后的子对象中

[英]JAVASCRIPT - Get Value from Parent Array of Objects, inside the Child Object after Array Flattening

I have an Array of Objects, which has some values.我有一个对象数组,它有一些值。 Examples:例子:

bigArray: [
  {
   greatName: 'Name String - #1',
   array: [{
     id: 1,
     name: 'string name - #1'
   }]
  },
  {
   greatName: 'Name String - #2',
   array: [{
     id: 2,
     name: 'string name - #2'
   }]
  },
  {
   greatName: 'Name String - #3',
   array: [{
     id: 3,
     name: 'string name - #3'
   }]
  }
]

I used the following composed functions from ramda, in order to get all Arrays array properties value and flatten them into a single array:我使用了 ramda 中的以下组合函数,以获取所有 Arrays array属性值并将它们展平为单个数组:

  const extractRB = compose(
    flatten,
    map(props(['array']))
  );

Which gave me back the following array:这给了我以下数组:

// Flatten Array:
flatten: [
  {
     id: 1,
     name: 'string name - #1'
   },
   {
     id: 2,
     name: 'string name - #2'
   },
   {
     id: 3,
     name: 'string name - #3'
   }
]

Now, that is a requirement, for my feature.现在,这是我的功能的要求。 THE PROBLEM is I also need the greatName , property value inside each object, in order to map through them.问题是我还需要每个对象中的greatName属性值,以便通过它们进行映射。 But I cannot find a way to actually get the correct values inside the object..但是我找不到一种方法来实际获取对象内部的正确值..

What I want to achieve is this Array of Objects.我想要实现的是这个对象数组。 Keep in mind that I can have dozens of Objects in Big Array, and hundrends of Objects inside each child array : Keep in mind that I can have dozens of Objects in Big Array, and hundrends of Objects inside each child array

// Flatten Array:
flatten: [
  {
     id: 1,
     name: 'string name - #1'
     greatName: 'Name String - #1',
   },
   {
     id: 2,
     name: 'string name - #2',
     greatName: 'Name String - #2',
   },
   {
     id: 3,
     name: 'string name - #3',
     greatName: 'Name String - #3',
   }
]

I did this, which is spreading the original object from the flatten array and add the greatName property to it, but how to calculate which is the correct value for each object.我这样做了,这是从展平数组中传播原始对象并向其添加greatName属性,但如何计算每个对象的正确值。 There is no common value between the object and the array to condition on:对象和数组之间没有共同的值作为条件:

flatten.map(flatObj =>  {...flatObj, greatName: bigArray.map(// what operation can I do here, to get the correct value for each object inside the `bigArray` )});

I tried quite a few conditional operation, but they will always return false.我尝试了很多条件操作,但它们总是返回 false。 Mayne there is a way to do it with the length of each array but not sure how to do it. Mayne 有一种方法可以处理每个array的长度,但不知道该怎么做。

Help would be greatly appreciated..!!帮助将不胜感激.. !! Thank you!!谢谢!!

 var bigArray= [ { greatName: 'Name String - #1', array: [{ id: 1, name: 'string name - #1' }] }, { greatName: 'Name String - #2', array: [{ id: 2, name: 'string name - #2' }] }, { greatName: 'Name String - #3', array: [{ id: 3, name: 'string name - #3' }] } ] console.log(bigArray.flatMap(({greatName, array}) => array.flatMap(x => ({greatName, ...x}))))

If array property always have only 1 element, so you can write it shorter:如果array属性总是只有 1 个元素,那么你可以把它写得更短:

  bigArray.map(({greatName, array}) => ({greatName, ...array[0]}))

Can you try this once, may be this will solve your problem:你能试试这个吗,也许这会解决你的问题:

bigArray.forEach((item)=>{
item.array.greatName  = item.greatName;
delete item.greatName;
})

Just console bigArray and don't use this and just try with above method只需控制台 bigArray,不要使用它,只需尝试使用上述方法

 const extractRB = compose(
    flatten,
    map(props(['array']))
  );

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

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