Home > Mobile >  How to add or give Id and some Random number to that key(Id) in JSON in react native
How to add or give Id and some Random number to that key(Id) in JSON in react native

Time:10-07

My Data Look Like This =

[
  {"name":"Wooden Furniture"},
  {"name":"Office Furniture"},
  {"name":"Kitchen Furniture"},
  {"name":"Modular Office Furniture"}
]

I want To Convert It Into Like This =

[
  {"name":"Wooden Furniture","id":"someRandomNumber"},
  {"name":"Office Furniture","id":"someRandomNumber"},
  {"name":"Kitchen Furniture","id":"someRandomNumber"},
  {"name":"Modular Office Furniture","id":"someRandomNumber"}
]

I just wanted To add Id:SomeRandomNumber in each object

CodePudding user response:

Using Array#map:

const arr = [{"name":"Wooden Furniture"}, {"name":"Office Furniture"}, {"name":"Kitchen Furniture"}, {"name":"Modular Office Furniture"} ];

const res = arr.map(o => ({ ...o, id: "SomeRandomNumber" }));

console.log(res);

  • Related