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

时间:2019-10-25 11:09:56

标签: javascript reactjs

我想从具有键的一个对象数组中获取值,并从具有相同键的另一个对象数组中获取值。

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')
  },
];

我尝试使用Object.keys从array1对象获取所有键。

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

,但是在某一点之后没有意义。

预期数组

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')
  }
]

我希望输出是键的值,然后将其输入到array2的“数字”键中。

5 个答案:

答案 0 :(得分:2)

您可以通过将key作为键的访问器来映射新数组。

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);

答案 1 :(得分:1)

以下代码可帮助您:-

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]
})

答案 2 :(得分:0)

您可以遍历array2中的每个对象,并添加从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)

答案 3 :(得分:0)

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);

您可以过滤以获得结果。

答案 4 :(得分:0)

这应该适合您。

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));