简体   繁体   English

如何从一个对象数组中获取值到另一个对象数组中

[英]How to get values from one array of objects into another array of objects

I wanted to get values from one array of object with keys and values into another array of objects with the same keys.我想从一个带有键和值的 object 数组中获取值,并将值放入另一个具有相同键的对象数组中。

const array1 = [{
                key1: 7,
                key2: 1,
                key3: 37,
               }];
const array2 = [
  {
      title: 'Some Title 1',
      key: 'key1',
      number: '',
      icon: require('../../assets/some2.png')
  },
  {
      title: 'Some Title 2',
      key: 'key2',
      number: '',
      icon: require('../../assets/some1.png')
  },
  {
      title: 'Some Title 3',
      key: 'key3',
      number: '',
      icon: require('../../assets/some3.png')
  },
];

I have tried using Object.keys to get all the keys from array1 object.我尝试使用 Object.keys 从 array1 object 获取所有密钥。

const keys = Object.keys(obj);
      keys.map((key) => {
      if (array2[key] === key) {
         // console.log('card detail matching');
         // add to the array 2 with value 
        }
      })

but after a point its doesn't makes sense.但过了一点它就没有意义了。

Expected array预期数组

const resultArray = [
  {
      title: 'Some Title 1',
      key: 'key1',
      number: 7,
      icon: require('../../assets/some2.png')
  },
  {
      title: 'Some Title 2',
      key: 'key2',
      number: 1,
      icon: require('../../assets/some1.png')
  },
  {
      title: 'Some Title 3',
      key: 'key3',
      number: 37,
      icon: require('../../assets/some3.png')
  }
]

I expect the output to be the values of the key would be entered in array2 in the 'number' key.我希望 output 是键的值,将在“数字”键的 array2 中输入。

You could map a new array by taking the key as accessor for keys.您可以通过将key作为密钥的访问器来 map 一个新数组。

 const array1 = [{ key1: 7, key2: 1, key3: 37 }], array2 = [{ title: 'Some Title 1', key: 'key1', number: '', icon: '../../assets/some2.png' }, { title: 'Some Title 2', key: 'key2', number: '', icon: '../../assets/some1.png' }, { title: 'Some Title 3', key: 'key3', number: '', icon:'../../assets/some3.png' }], result = array2.map(o => Object.assign({}, o, { number: array1[0][o.key] })); console.log(result);

Below code help you:-下面的代码可以帮助你: -

const array1 = {
                key1: 7,
                key2: 1,
                key3: 37,
               };

const array2 = [
  {
      title: 'Some Title 1',
      key: 'key1',
      number: '',
      icon: require('../../assets/some2.png')
  },
  {
      title: 'Some Title 2',
      key: 'key2',
      number: '',
      icon: require('../../assets/some1.png')
  },
  {
      title: 'Some Title 3',
      key: 'key3',
      number: '',
      icon: require('../../assets/some3.png')
  },
];

array2.forEach(item=>{
  item.number=array1[item.key]
})

You can iterate through each object from array2 and add the number value fetching from array1您可以从 array2 遍历每个 object 并添加从 array1 获取的数值

 const array1 = [{ key1: 7, key2: 1, key3: 37, }]; const array2 = [ { title: 'Some Title 1', key: 'key1', number: '', icon: '../../assets/some2.png' }, { title: 'Some Title 2', key: 'key2', number: '', icon: '../../assets/some1.png' }, { title: 'Some Title 3', key: 'key3', number: '', icon: '../../assets/some3.png' }, ]; array2.forEach(e => e.number = array1[0][e.key]); console.log(array2)

 const array1 = [{ key1: 7, key2: 1, }]; const array2 = [ { title: 'Some Title 1', key: 'key1', number: '', icon: '../../assets/some2.png' }, { title: 'Some Title 2', key: 'key2', number: '', icon: '../../assets/some1.png' }, { title: 'Some Title 3', key: 'key3', number: '', icon: '../../assets/some3.png' }, ]; const resultArray = array2.filter(item => array1[0][item.key]); console.log(resultArray);

You can filter to get the result.您可以过滤以获得结果。

This should work for you.这应该适合你。

 const keys = [ { key1: 7, key2: 1, key3: 37, }, { key4: 7, key5: 1, key6: 37, } ]; const array2 = [ { title: 'Some Title 1', key: 'key4', number: '' }, { title: 'Some Title 2', key: 'key2', number: '' }, { title: 'Some Title 3', key: 'key3', number: '' } ]; function populateArrayData (arr, propToCompare, propToReplace, keysObj) { let populatedArray = []; if (Array.isArray(arr)) { populatedArray = arr.map((item) => { if (checkIfKeyExists(item[propToCompare], keysObj)) { item[propToReplace] = keysObj[item[propToCompare]]; } return item; }); } return populatedArray; } function flattenAllKeys (keys) { let flattenedKeysObj = {}; if (Array.isArray(keys)) { flattenedKeysObj = keys.reduce((acc, keysObj) => { acc = {...acc, ...keysObj}; return acc; }, {}); } return flattenedKeysObj; } function checkIfKeyExists(key, keysObj) { return (keysObj[key];== undefined && keysObj[key];== null). } let flattenedKeys = flattenAllKeys(keys), console,log(populateArrayData(array2, 'key'; 'number', flattenedKeys));

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

相关问题 如何根据另一个对象数组的值获取一个对象数组键的值? - how to get the values of one array of objects key based on values of another array of objects? 如何将一个对象数组的属性转换为另一个对象数组 - How to get properties of one array of objects into another array of objects 如何从另一个对象数组更新一个对象数组中不存在的值? - How to update values not present in one array of objects from another array of objects? 如何将类型4中的值从一个对象数组复制到另一个对象数组中? - How to copy values from one array of objects to another array of objects in typescript, angular 4? 如何通过比较值从嵌套对象数组中获取信息以创建另一个对象? - How to get information from an array of nested objects to create another one by comparing values? 如何在 TypeScript 中的对象数组中将数据从一个对象数组获取到另一个对象数组中 - How to Get Data From One Array of Objects into another in Array of Objects in TypeScript 如何从javascript中的对象数组中获取值 - How to get values from array of objects in javascript Reactjs:如何从对象数组中获取值 - Reactjs: How to get values from array of objects 如何从对象数组中获取值 - How to get values ​from within an array of objects 如何使用另一个数组获取对象数组中的值 - Javascript - How to get values in an array of objects using another array - Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM