1

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.

1
  • the code you wrote does nothing, so how does it not make sense? Commented Oct 25, 2019 at 11:11

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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)

Comments

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

You can filter to get the result.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.