2

"I have an array:

myArray = []

I'm looping through some data and adding it to my array:

myArray.push(listItem)

I would then like to add an array of objects to this array. The end result is that inside each of these objects is an array of image paths

0:
id: "8okCSIfrSTugwBW4Oa0Q"
images: [{…}]
title: "My Title 1"
1:
id: "8okCSIfrSTugwBW4Oa023"
images: [{…}]
title: "My Title 2"

I've tried the following:

var newIndex = index - 1;
var imagesArray = [];
imagesArray.push(imageString);
this.myArray[newIndex]["images"] = imagesArray;
3
  • 2
    And what is the problem you are facing? Commented Dec 2, 2019 at 12:17
  • @mplungjan it works in both cases, .key works but with ["key"] it doesnot, just checked Commented Dec 2, 2019 at 12:24
  • what do you want from this array ? Commented Dec 2, 2019 at 12:54

1 Answer 1

4

Just find a desired object in yourArray and assign a desired value:

let yourArray = [];

yourArray.push({id: "8okCSIfrSTugwBW4Oa0Q", title: "My Title 1"});
yourArray.push({id: "8okCSIfrSTugwBW4Oa023", title: "My Title 2"});

yourArray.find(f=>f.id = "8okCSIfrSTugwBW4Oa0Q").images = [];

console.log(yourArray);

Or safer version with checking whether desired object is existing:

let yourArray = [];

yourArray.push({id: "8okCSIfrSTugwBW4Oa0Q", title: "My Title 1"});
yourArray.push({id: "8okCSIfrSTugwBW4Oa023", title: "My Title 2"});

let desiredObject = yourArray.find(f=>f.id = "8okCSIfrSTugwBW4Oa0Q");
if (desiredObject)
    desiredObject.images = [];

console.log(yourArray);

UPDATE:

If you dont' have access to the ID in the addImage function, then this is the way via index:

yourArray[0].images = [];

In addition, you can find index via findIndex method:

let yourArray = [];

yourArray.push({id: "8okCSIfrSTugwBW4Oa0Q", title: "My Title 1"});
yourArray.push({id: "8okCSIfrSTugwBW4Oa023", title: "My Title 2"});


let index = yourArray.findIndex(f => f.title == 'My Title 1');
if (index != -1)
    yourArray[index].images = [];

console.log(yourArray);

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

7 Comments

Thanks for the response! - Unfortunately, I don't have access to the ID in the addImage function, which is why I was trying to do it via index...if that makes any sense.
myArray[0].images = [];
Thanks for the rapid response. The issue is that I don't have access to that data to search for the index. Unfortunately I cannot match up the titles, IDs or anything like that. I may have 200 items, some with 1 image, some with 20. I don't know.
When I push an item into the array, it looks like it's returned as a string: 0: "21,0,1,"Issue 1",,"","I have some comments that are ↵↵going here on multiple lines",,"2019/11/28, 12:20:55","",""↵" 1: "23,1,2,"Issue 2",,"","",,"2019/12/02, 11:37:38","",""↵" - So when I try something like yourArray[0].images = [];, it states ` Cannot create property 'images' on string`
@que so you can create images like that: yourArray[0].images = []; where 0 is array index.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.