0

I need to create a new object key/value. In it i need the values from the already existing key/value object and i need to add values that are only in the array.

The array:

[{ name: "Computer", name: "Car", name: "House", name: "Flower"}]

The object key/value:

{ computer: { title: 'Computer', color: 'Black' }, house: { title: 'House', color: 'Red' }}

So in this case the new object would need to be:

{ computer: { title: 'Computer', color: 'Black' }, car: { title: 'Car', color:'' }, house: { title: 'House', color: 'Red' }, flower: { title: 'Flower', color:'' } } 

What is the easiest way to achieve this? I was thinking to loop over the array compare the values between them and extract the repeating ones, but i'm not sure how to do that.

8
  • That new object isn’t syntactically valid. It’s not clear to me how you expect this to work in any case… the key computer is arbitrary? Are 1 and 2 meant to be literal keys corresponding to … what, the array index plus one? Is the name key arbitrary? I would have expected {title: "name"} or something. I am very confused! Commented Nov 4, 2021 at 16:48
  • @jcalz it can be same object as the previous one, i've updated it. Does it make more sense now? Commented Nov 4, 2021 at 20:25
  • Not really, sorry. It would be helpful if your code is a minimal reproducible example that, when you drop into a standalone IDE like The TypeScript Playground (click this link!), clearly demonstrates your issue. Your edited object is still not a valid JavaScript object; it has syntax errors. And I still don't understand what operation you're trying to perform here. The "car" key seems to come out of nowhere. Could you please spell out the relationship? Maybe post a few more examples of input/output? Commented Nov 5, 2021 at 3:22
  • @jcalz I found my mistake it was a set of extra brackets {}, please have a look now here Commented Nov 5, 2021 at 12:25
  • Just in case you missed it: I still don't understand what operation you're trying to perform here. The car key seems to come out of nowhere. Could you please spell out the relationship? Maybe post a few more examples of input/output? From this one example it's difficult to tell which parts are static and which parts are dynamic. Like, do we always copy from the name property of the elements of the array? Or is name arbitrary and it might be different in another array? Commented Nov 5, 2021 at 12:54

1 Answer 1

1

I've made the assumption that that first array was meant to be an array of individual objects each with a key of 'name' as in the arr array.

const arr = [ {name: "Computer"}, {name: "Car"}, {name: "House"}, {name: "Flower"}];
const objVal = { 
        computer: { title: 'Computer', color: 'Black' }, 
        house: { title: 'House', color: 'Red' }
        };
const expected = { 
        computer: { title: 'Computer', color: 'Black' }, 
        car: { title: 'Car', color:'' }, 
        house: { title: 'House', color: 'Red' }, 
        flower: { title: 'Flower', color:'' } 
        } 

let returnObj = {};

arr.forEach((element) => {
    const name = element.name;
    if (objVal[name.toLowerCase()]) {
        returnObj[name.toLowerCase()] = objVal[name.toLowerCase()];
    } else {
      returnObj[name.toLowerCase()] = {
        title: name,
        color: ''
      }
    }
});

console.log(returnObj);
Sign up to request clarification or add additional context in comments.

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.