0

I want to add a key and value(i.e. age:15) to an object which has name as email and remove it(age) from other objects for the below array of object.

[
  {
    name: 'test',
    lname: 'last',
    age: 5
  },
  {
    name: 'test1',
    lname: 'last1',
    age: 15
  },
  {
    name: 'email',
    lname: 'last',
  },
]

i.e. I want the below output.

[
  {
    name: 'test',
    lname: 'last'
  },
  {
    name: 'test1',
    lname: 'last1'
  },
  {
    name: 'email',
    lname: 'last',
    age: 15
  },
]

Thanks

Benk I
  • 185
  • 13
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). What exactly is the problem? Where in the process of writing the code are you stuck? – Sebastian Simon Dec 17 '21 at 12:03
  • 1
    Please donot post a requirement without any valid attemepts or mentioning the difficlty that you are facing. Please not SO is not a freelance website, where you can post the requirement and someone will work and povide you the solution. We are here to help when you face any and not to write entire code for you. – Nitheesh Dec 17 '21 at 12:29

3 Answers3

0

What you can do here is find the index of the object that has name as "email". Once you find the object, add the desired age value as a new property called age. Finally, you can use filter to filter the items that doesn't have name as "email" and delete the age property.

var data = [ { name: 'test', lname: 'last', age: 5 }, { name: 'test1', lname: 'last1', age: 15 }, { name: 'email', lname: 'last', }, ]

function myFunction(age) {
  let indexOfEmail = data.findIndex(element => element.name == "email")
  if (indexOfEmail > -1) {
    data[indexOfEmail].age = age
    data.filter(element => element.name !== "email").map(sub => delete sub['age'])
  }
}

myFunction(15)
console.log(data)
Deepak
  • 2,660
  • 2
  • 8
  • 23
  • 1
    Please try to avoid answering a question where the user has not tried anything to acheve the result. – Nitheesh Dec 17 '21 at 12:30
0

You can do it by using map method, like this:

const data = [
  {
    name: 'test',
    lname: 'last',
    age: 5
  },
  {
    name: 'test1',
    lname: 'last1',
    age: 15
  },
  {
    name: 'email',
    lname: 'last',
  },
];
const newData = data.map(({age, ...rest})=> rest.name == 'email' ? {...rest, age: 15} : rest)
console.log(newData);
Saeed Shamloo
  • 6,199
  • 1
  • 7
  • 18
0

You can do like this:


const items = [
  {
    name: 'test',
    lname: 'last',
    age: 5
  },
  {
    name: 'test1',
    lname: 'last1',
    age: 15
  },
  {
    name: 'email',
    lname: 'last',
  },
];

const newItems = items.filter((item) => {
    if (item.name.includes("email")) {
      return (item.age = 15);
    }
    if (JSON.stringify(items).includes("email")) {
      delete item.age;
    }
    return item;
});

console.log(newItems);
kiran
  • 444
  • 3
  • 15
  • 1
    Please try to avoid answering a question where the user has not tried anything to acheve the result. – Nitheesh Dec 17 '21 at 12:30