1

Hello I m trying to map through a json file and add a new property to every object in it.

DATA.JSON

const arr = [
  {
    index: 0,
    name: "John",
    hair: "Brown",
  },
  {
    index: 1,
    name: "Bob",
    hair: "Blonde",
  },
];

APP.JS

const data = require("./data.json");

const items = data.map((item) => {
  return ????
});



I want to map through the array, and add a "age" property to every index with a value of 30 or even just an empty value would be ok. with result something like this:

RESULT
const a =
[
  {
    index: 0,
    name: "John",
    hair: "Brown",
    age: 30
  },
  {
    index: 1,
    name: "Bob",
    hair: "Blonde",
    age: 30 
  },
];


DecPK
  • 24,537
  • 6
  • 26
  • 42
lache
  • 608
  • 2
  • 12
  • 29
  • 1
    The `item` plus whatever data you want, e.g., `{ ...item, age: 30}`? Although if you explicitly want to modify the list *in place* you'd want to use `forEach`. It's not clear to me what the confusion is here. – Dave Newton May 05 '21 at 12:38
  • I see! thank you so much I am new to json stuff. I need the spread operator or a forEach then – lache May 05 '21 at 12:41
  • [There is no JSON here](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). See also: [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/q/2904131) – VLAZ May 05 '21 at 12:43
  • I will check that out thank you they seem so similar as a newbie – lache May 05 '21 at 12:55

2 Answers2

2

const arr = [
  {
    index: 0,
    name: "John",
    hair: "Brown",
  },
  {
    index: 1,
    name: "Bob",
    hair: "Blonde",
  },
];

const result = arr.map((obj) => {
  return { ...obj, age: 30 };
});

console.log(result);

You can also make it one-liner

const result = arr.map((obj) => ({ ...obj, age: 30 }));

If you want to print only hair of each index then

const arr = [
  {
    index: 0,
    name: "John",
    hair: "Brown",
  },
  {
    index: 1,
    name: "Bob",
    hair: "Blonde",
  },
];

const result = arr.map((obj) => {
  return { ...obj, age: 30 };
});

const hairs = result.map((o) => o.hair);
console.log(hairs);  // Printing as an array
console.log(hairs.toString()); // Printing as comma separated value
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • what if i want to return certain values to the console at the same time? Like print the value of hair of each index to the console? or the new age property instead of the entire "result" – lache May 05 '21 at 13:03
1

Hello you could do something like this:

const data = [
  {
    index:0,
    name: "John",
    hair: "Brown",
  },
  {
    index:1,
    name: "Bob",
    hair: "Blonde",
  },
];


const newData = data.map(item => ({
  ...item,
  newProp: 'hello world',
}));


// Print whole data:
console.log(newData);

// Filter specific properties 
const hair = newData.map(item => item['hair'])
console.log('only hair ->', hair);

Side note, you don't need the index property in the object since you can get it while looping.

map(currentItem, index) Docs

If you want to print specific properties of the object you can do as such: console.log(newData.map(item => item['hair']));

Tiago
  • 358
  • 2
  • 14
  • what if i want to show certain values to the console at the same time? Like print the value of hair of each index to the console? instead of the entire "newData" – lache May 05 '21 at 13:06
  • 1
    Example updated with ```console.log(newData.map(item => item['hair']));``` – Tiago May 05 '21 at 13:10