0

I have an array

const arr = ['a', 'b', 'd', 'c', 'e'];

The API sends this data

const data ={a:'a', b:'b', c:'c', e:'e', f:'f'}

sometimes the keys are missing as well but the array keys should always be present in the final result.

the output should be {a:'a', b:'b', d:'', c:'c', e:'e'}. Please help me with ES6 and give me an explanation. I am using reactjs16.9. In this given case the object d is missing hence it should be initialised with "". In case If all the keys exist then extra keys from API should be removed.

4
  • Object keys are not ordered Commented Apr 16, 2020 at 11:05
  • 2
    Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output. Commented Apr 16, 2020 at 11:06
  • @ZohaibIjaz I have to extract the values into an array which should come as per the array if the object is not ordered I cant do that. Commented Apr 16, 2020 at 17:42
  • @mplungjan I did my research then only decided to ask the question. The actual code is copyrighted hence had to form an example for that. Commented Apr 16, 2020 at 17:44

2 Answers 2

2

You could iterate the keys and build a new object by using only wanted keys.

Inside of the callback for mapping, the existence of the key is checked with the in operator and a conditional (ternary) operator ?: is used for getting either the value from the object or a placeholder ' ' for unknown properties.

The result is either an object with Object.fromEntries or a simple array.

const
    keys = ['a', 'b', 'd', 'c', 'e'],
    data = {a: 'a', b: 'b', c: 'c', e: 'e', f: 'f' },
    result = Object.fromEntries(keys.map(k => [k, k in data ? data[k] : ' '])),
    onlyValues = keys.map(k => k in data ? data[k] : ' ');

console.log(result);
console.log(onlyValues);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

5 Comments

What if the d key doesn't exist It should take " " and add that key from that array. The need for ordered result is because the values are to be extracted in the order of array.
what if I wanted to extract only the values from the final object as an array?
why not take an array for it? if you still like to use an object, take Object.values(result).
can you please add an explanation for your answer? So that they could reference your answer for future as well?
I did @mplungjan but Nina has got it all bang on. I understand her answer Its for others as well. You should also add explanation in the answer.
0

Perhaps this?

Using || to use space if no entry available

const arr = ['a', 'b', 'd', 'c', 'e'];
const data = {a:'a', b:'b', c:'c', e:'e', f:'f'};

let newObj = {};
let newArr = [];
arr.forEach(item => {
  newObj[item] = data[item] || " ";
  newArr.push(   data[item] || " ");
})
console.log(newObj);
console.log(newArr);

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.