0

I have an array of objects that looks like this:

[
   {
      "id": 123,
      "timeStamp": "\"2019-07-08T20:36:41.580Z\"",
      "data": [1, 2, 3]
   },
   {
      "id": 234,
      "timeStamp": "\"2019-07-08T20:37:12.472Z\"",
      "data": ["Apples", "Oranges"]
   }
]

I want to update the value of a particular property of an object within the array but also want to make sure that I return the result in a new array.

How do I do this without running through some type of a loop e.g. for loop?

Say, I want to update the data property of the second object and add Bananas to it.

2
  • 1
    You will need some kind of loop to iterate through your array: it will be either explicit or implicit. Commented Jul 8, 2019 at 20:47
  • You're going to need to loop through this array one way or another if you want to keep them in array form, unless you keep track of the index of each object in the array, at which point you're just using an object with extra steps. Commented Jul 8, 2019 at 20:47

3 Answers 3

2

If you want the result to be a new array, you'll first have to clone the array. This can be more complicated than you might imagine (depending on how deeply you wish to clone things). One way is to use JSON stringify...

Bear in mind that the JSON trick is effectively doing a loop behind the scenes. Inevitable if you want to copy the array, really.

To find the object by ID use Array.find()

let original = [
   {
      "id": 123,
      "timeStamp": "\"2019-07-08T20:36:41.580Z\"",
      "data": [1, 2, 3]
   },
   {
      "id": 234,
      "timeStamp": "\"2019-07-08T20:37:12.472Z\"",
      "data": ["Apples", "Oranges"]
   }
]

let copy = JSON.parse(JSON.stringify(original));

copy.find(obj => obj.id === 234).data.push("Bananas");

console.log(copy);
    
 

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

7 Comments

This is very very close to what I want but I may not always know the index of the object I want to update.
Well how do you plan on identifying the object you wish to modify? By id?
I'd like to use the id property to identify the object that I need to update
See the edit I've made – you can use Array.find to get the first element in an array that has a specific id (here I set the id to 234 but presumably id will be a variable passed to a function)
Excellent! Thank you! I assume changing the value is simply an assignment e.g. copy.find(obj => obj.id === 234).timeStamp = JSON.stringify(new Date());
|
0

Something like this would do the trick:

 let arr = [
    {
     "id": 123,
     "timeStamp": "\"2019-07-08T20:36:41.580Z\"",
     "data": [1, 2, 3]
    },
    {
     "id": 234,
     "timeStamp": "\"2019-07-08T20:37:12.472Z\"",
     "data": ["Apples", "Oranges"]
     }
  ]

 arr[1]['data'] = [...arr[1]['data'], 'Bananas']

 console.log(arr)

1 Comment

"... but also want to make sure that I return the result in a new array."
-1

For your example: you can do something like this: say your array of object is saved in test variable

test[1].data.push("Bananas")

1 Comment

Does not answer OP question at all.

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.