Swapnil Srivastava
Swapnil Srivastava

Reputation: 173

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.

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.

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.

Upvotes: 1

Views: 1115

Answers (5)

Kapil Bhagat
Kapil Bhagat

Reputation: 51

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

Upvotes: 0

Raja Sekar
Raja Sekar

Reputation: 2130

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.

Upvotes: 0

raman mathur
raman mathur

Reputation: 124

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

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386848

You could map a new array by taking the key as accessor for keys.

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

Upvotes: 2

arizafar
arizafar

Reputation: 3132

You can iterate through each object from array2 and add the number value fetching from 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)

Upvotes: 0

Related Questions