0

Currently I have an array object[{}] code for the which is

const updatedDat = response.data
const updatedData = []
for (let i=0; i<5; i++) {
    updatedData.push(updatedDat[i])
}
console.log(updatedData)

The console.log above logs

Array(5)
0:{userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit↵suscipit recusandae consequuntur …strum rerum est autem sunt rem eveniet architecto"}

1:{userId: 1, id: 2, title: "qui est esse", body: "est rerum tempore vitae↵sequi sint nihil reprehend…aperiam non debitis possimus qui neque nisi nulla"}

2:{userId: 1, id: 3, title: "ea molestias quasi exercitationem repellat qui ipsa sit aut", body: "et iusto sed quo iure↵voluptatem occaecati omnis e…↵molestiae porro eius odio et labore et velit aut"}

3:{userId: 1, id: 4, title: "eum et est occaecati", body: "ullam et saepe reiciendis voluptatem adipisci↵sit … ipsam iure↵quis sunt voluptatem rerum illo velit"}

4:{userId: 1, id: 5, title: "nesciunt quas odio", body: "repudiandae veniam quaerat sunt sed↵alias aut fugi…sse voluptatibus quis↵est aut tenetur dolor neque"}
length:5

Now I want to add another field inside this object named author hence this should look like this

1:{Author:"Max" userId: 1, id: 2, title: "qui est esse", body: "est rerum tempore vitae↵sequi sint nihil reprehend…aperiam non debitis possimus qui neque nisi nulla"}

How can I achieve that?

3
  • 1
    updatedDat[i].Author = "Max"; Commented Jun 15, 2018 at 18:23
  • 1
    Maybe you want updatedDat[i].author = "Max" ? Is the name supposed to be the same for all objects? Commented Jun 15, 2018 at 18:23
  • The same way you add a property to any other object. Why do you think this is complicated? Commented Jun 15, 2018 at 18:24

4 Answers 4

1

below code should be help you if we have to add property which in array element then must be add in 0 index

const updatedDat = [{id:"1"}];
  updatedDat[0].auther = "some name"
    const updatedData = []
    for (let i=0; i<updatedDat.length; i++) {
        updatedData.push(updatedDat[i])
    }
    console.log(updatedData)

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

Comments

1

You can achieve it by a simple forEach loop:

updatedData.forEach((obj)=>{
  obj.Author = "max";
});

Comments

1

Assuming you want to add that key to a specific object in your array:

let arr = [
  {name: 'will', id: 1},
  {name: 'bill', id: 2}
];

const foundItem = arr.find(item => item.id === 1);
foundItem.author = 'foo';

console.log(arr);

Comments

1

In your for loop add the property before inserting it.

for (let i=0; i<5; i++) {
    updatedDat[i].Author = "Max";
    updatedData.push(updatedDat[i])
}

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.