2

i'm new to javascript I have an array of objects like

arr=[{id:1,likes:10},{id:3,likes:13},{id:3,likes:1}]

what if i want to increase the value of likes by 1 where id is 1 and store the resulting array in arr2 ???

arr2=[{id:1,likes:11},{id:3,likes:13},{id:3,likes:1}]

2
  • I am trying to do this using spread operator(...) Commented Dec 3, 2019 at 6:47
  • Please edit your question and add more details like what's problem stoping you? Commented Dec 3, 2019 at 6:50

7 Answers 7

1

You can Do like this

const arr = [{ id: 1, likes: 10 }, { id: 3, likes: 13 }, { id: 3, likes: 1 }]
const arr2 = []
let obj = {}
for (let i = 0; i < arr.length; i++) {
    if (arr[i]['id'] === 1)
        obj = { id: arr[i]['id'], likes: arr[i]['likes'] + 1 }
    else
        obj = arr[i]

    arr2.push(obj)
}
console.log("arr",arr) 
console.log("arr2",arr2)

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

6 Comments

downvoters, please dare to comment, what's wrong so that i could know, I think my answer is right.
though I haven't downvoted your reply, your code looks clean to me but just a suggestion, store the result in new array as mentioned in question.!
one more thing, try to use ES6 standards, use const / let according to usage instead of traditional var
Happy to help you.! @Narendra
@NarendraChouhan your answer is wrong because it mutates the original array as well.
|
1

Using map()

const arr = [{id:1,likes:10},{id:3,likes:13},{id:3,likes:1}]

const arr2 = arr.map(i => (i.id === 1 && (i.likes += 1), i))

console.log(arr2)

Comments

1
const arr = [{id:1,likes:10},{id:3,likes:13},{id:3,likes:1}];
const arr2 = arr.map(obj =>
  obj.id === 1 ?
    { id: obj.id, likes: obj.likes + 1 } :
    obj
); 

or maybe you want the id to be dynamic?

const increment_likes = (id, old) => old.map(obj =>
  obj.id === id ?
    { id: obj.id, likes: obj.likes + 1 } :
    obj
);
const arr2 = increment_likes(1, arr);

Comments

1

You can do in this way.

arr=[{id:1,likes:10},{id:3,likes:13},{id:5,likes:1}]
function increase_id_likes_by_one(id){
    for(i=0;i<arr.length;i++){
    if (arr[i]['id'] == id) {
        arr[i]['likes'] += 1;
    }
  }
  console.log(arr);
}
increase_id_likes_by_one(1);
increase_id_likes_by_one(3);
increase_id_likes_by_one(5);
increase_id_likes_by_one(5);

The function increase_id_likes_by_one will take id as argument and print the new array ( after increasing the count ).

Comments

1

You can use Array.map():

var arr=[{id:1,likes:10},{id:3,likes:13},{id:3,likes:1}];

var id = 1

var res = arr.map((obj) => {
  obj.id === 1? obj.likes++: obj.likes;
  return obj;
});
console.log(res);

Comments

1

An inline version of map method:

var arr=[{id:1,likes:10},{id:3,likes:13},{id:3,likes:1}];

var id = 1

var res = arr.map(({id, likes}) => ({  
  id,
  likes : (id === 1? ++likes : likes)
}));
console.log(res);

Comments

1

You can try with JSON.stringify() and JSON.parse() with Array.prototype.map():

var arr=[{id:1,likes:10},{id:3,likes:13},{id:3,likes:1}];

var arr2 = JSON.parse(JSON.stringify(arr)).map(i => {
  if(i.id==1) i.likes += 1;
  return i;
});
console.log(arr); //original array without modification
console.log(arr2); //new array with modification

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.