0

I want to assign a property to an object, that is part of an array of objects. The value shall be the value of an existing property of the object. How is this done?

    const data = [ {id: 111, name: 'AAA'}, ...];

    const result= data.map(item => {
      item.copyOfId= item.id;
      return item;
    });
2
  • So you just need the index of the object in the array as the value of copyOfId? Commented Nov 28, 2017 at 16:01
  • For reference: an array of objects is referred to as a collection Commented Nov 28, 2017 at 16:11

3 Answers 3

4

There's no need for map there, you just need to loop through the array, not map the array. So all the usual ways of looping through arrays apply, such as forEach:

const data = [ {id: 111, name: 'AAA'}, ...];
data.forEach(item => {
  item.copyOfId = item.id;
});

or for-of:

const data = [ {id: 111, name: 'AAA'}, ...];
for (const item of data) {
  item.copyOfId = item.id;
}

As Mörre points out in a comment, in functional programming map is indeed what you would reach for, but usually you'd create new objects to populate the new array, instead of modifying (mutating) the objects:

const data = [ {id: 111, name: 'AAA'}, ...];
const result = data.map(item => {
  return {...item, copyOfId: item.id};
});

(That example uses spread properties, which are only a Stage 3 proposal but are supported by up-to-date Chrome and Firefox. You could use Object.assign instead. Either way, it's a shallow copy if that's important.)

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

3 Comments

To elaborate on the map, it would be fine - IF he produced a completely new array (functional programming style, no mutations, etc.). But producing a new array while still mutating the content items - that's a conflicting message. Either all new (use map), or mutate (use forEach). Note that sure, you can use either one, it's just that there are certain associations/assumptions associated with those methods, which come from other languages.
@cale_b: for-of, arrow functions, and const are, forEach isn't. I'd say the question doesn't relate to ES2015 ("ES6") specifically, so probably not.
@T.J.Crowder sorry, the index was wrong and i now corrected in the question, thanks for the fast answer
3

A slightly different and shorter version (using object destructuring) would look like so:

const data = [ {id: 111, name: 'AAA'}, ...];

const result = data.map(({ id, name }) => ({ id, name, copyOfId: id }));

And using spread syntax:

const result = data.map(item => ({ ...item, copyOfId: item.id }));

Previous solutions work just as well, but I thought you may be interested in some additional ES2015 features.

Comments

2

You can use map and within each iteration, you can use the spread operator to create a duplicate BUT new object.

const data = [{
  id: 111,
  name: 'AAA'
}];

const result = data.map((item) => {      
  return { ...item, copyOfId: item.id };
});

console.log(result);

6 Comments

Why? Doesn't he want a new array of objects?
Even if OP wanted new array of objects your answer is still wrong because it will produce new array of ids.
Where am I mutating the array though? Is this not pure?
You mutate the items in the array. Which still are the same ones in the old array, because those are object references. EDIT: Before the edit.
@CarlEdwards - For what it's worth, I enjoy participating in SO (including answering) because I'm always learning from people way smarter than me. So don't be discouraged by these comments - they are an invaluable education (which I'm learning from!)
|

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.