How to concatenate 2 arrays by index order? [duplicate] - javascript

This question already has answers here:
Javascript equivalent of Python's zip function
(24 answers)
Closed 3 years ago.
Im trying to figure out how to use highcharts with the dates. Im doing an API call and have in one variable clicks and in another variable the dates. I want to concatenate these 2 arrays but following the index.
For example
array1 = [timestamp1 , timestamp2, timestamp3]
array2 =[ clicks1, clicks2, clicks3 ]
I want to have then an array that is
data = 0: [timestamp1, click1],
1: [timestamp2, click2],
2: [timestamp3, click3]
I tried doing var data = array1.concat(array2);
but it concatenates in order
data = [timestamp1 , timestamp2, timestamp3 , clicks1, clicks2, clicks3 ]
my original code looks like this:
getChartData(){
axios.get("http://localhost:3003/desktop").then(api =>{
const data = api.data;
// console.log(data)
var date =[];
var clicks = []
var impressions =[]
for ( var i in data)
{
date.push(data[i].Date)
clicks.push(data[i].Clicks)
impressions.push(data[i].Impressions)
}
var cd = date.concat(clicks);
console.log(cd)
this.setState({
clicks:clicks,
date:date,
impressions:impressions,
cd: cd
})
});
}
This might be a very basic question, but I'm a beginner and could not find anything related.Any help would be appreciated.

Assuming that both arrays are the same length you could use something like this. You can do the iteration a variety of ways.
const arr1 = ['a', 'b', 'c'];
const arr2 = ['A', 'B', 'C'];
const arrPaired = arr1.map((ent, i) => [ent, arr2[i]]);
console.log(arrPaired);

You could parse all the keys of one of your array and then retrieve the value from both array using that key. Ex:
let output = []
Object.keys(array1).forEach((key) => {
output.push(array1[key], array2[key]);
});
Here i'm using the forEach array function but you could achieve the same goal using a normal foreach loop.
Please note that this code is untested, it is just to show you the basic idea.

Related

array.push() and splite in Array of arrays [duplicate]

This question already has answers here:
Split array into chunks
(73 answers)
Closed 6 days ago.
This post was edited and submitted for review 6 days ago.
I'm using arrayofArrays[0].push() inside a for loop in order to add an Object to the Array0 inside arrayofArrays.
Now I'd like that when Array0 has reached 2 element, the next element will be pushed to Array1, in order to achieve this situation:
var arrayofArrays = [[Obj0,Obj1],[Obj2,Obj3],[Obj4,Obj5], ...];
Sample code:
var arrayofArrays = [[]];
for(data in Data){
var Obj = {"field1":data[0], "field2":data[1], "field3":data[2] }
arrayofArrays[0].push(Obj); // need to pass to arrayofArrays[1] when arrayofArrays[0] has 2 Obj...
}
(I don't need to split an existing array, I'm adding Object to an array, and want them to split in sub-arrays while adding them)
Here is a functional programming approach to your question to unflatten an array:
const arr = Array.from(Array(6).keys()); // [0, 1, 2, 3...]
let t;
let arrOfArr = arr.map((v, idx) => {
if(idx % 2) {
return [t, v];
} else {
t = v;
return null;
}
}).filter(Boolean);
console.log({
arr,
arrOfArr,
});
Note: Array.from(Array(6).keys()) is just for demo. Replace this with any array of objects you like

SUM AND GROUPING JSON DATA USING JAVASCRIPT [duplicate]

This question already has answers here:
How to group by and sum an array of objects? [duplicate]
(2 answers)
Sum JavaScript object propertyA values with the same object propertyB in an array of objects
(12 answers)
Group by, and sum, and generate an object for each array in JavaScript
(4 answers)
ES6 Implementation of Group By and SUM
(4 answers)
Javascript array of objects group and sum items
(4 answers)
Closed last year.
Sorry if this has been asked before, but I couldn't find a good example of what I'm trying to accomplish. Maybe I'm just not searching for the right thing. Please correct me if there's an explanation of this somewhere.
so let's says I have a data like this :
data = [
{"no":1,"location":"New York","transaction":3000},
{"no":2,"location":"Tokyo","transaction":3000},
{"no":3,"location":"New York","transaction":3000},
{"no":4,"location":"Amsterdam","transaction":3000},
{"no":5,"location":"Manchester","transaction":3000},
{"no":6,"location":"New York","transaction":3000},
{"no":7,"location":"Tokyo","transaction":3000},
{"no":8,"location":"Tokyo","transaction":3000},
{"no":9,"location":"New York","transaction":3000},
{"no":10,"location":"Amsterdam","transaction":3000}
]
what i wanted to is an output like this :
result = [
{"location":"New York","transaction":12000},
{"location":"Tokyo","transaction":9000},
{"location":"Amsterdam","transaction":6000}
{"location":"Manchester","transaction":3000}
]
so what i wanted to do is grouping the data based on location and sum the transaction where the location is same and push the data to another array. i don't know where to start, need some help to solve this or any suggestion to solve this using Javascript. thank you
Working Demo :
// Input array
const data = [
{"no":1,"location":"New York","transaction":3000},
{"no":2,"location":"Tokyo","transaction":3000},
{"no":3,"location":"New York","transaction":3000},
{"no":4,"location":"Amsterdam","transaction":3000},
{"no":5,"location":"Manchester","transaction":3000},
{"no":6,"location":"New York","transaction":3000},
{"no":7,"location":"Tokyo","transaction":3000},
{"no":8,"location":"Tokyo","transaction":3000},
{"no":9,"location":"New York","transaction":3000},
{"no":10,"location":"Amsterdam","transaction":3000}
];
// result array
const resultArr = [];
// grouping by location and resulting with an object using Array.reduce() method
const groupByLocation = data.reduce((group, item) => {
const { location } = item;
group[location] = group[location] ?? [];
group[location].push(item.transaction);
return group;
}, {});
// Finally calculating the sum based on the location array we have.
Object.keys(groupByLocation).forEach((item) => {
groupByLocation[item] = groupByLocation[item].reduce((a, b) => a + b);
resultArr.push({
'location': item,
'transaction': groupByLocation[item]
})
})
console.log(resultArr)

How to get the years from an array of dates in order

I have this array:
var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];
How can I get the years from it in order? So the result would be this:
result = ['2020','2021','2022'];
Do I have to change the array to a new Date() format and sort, or how would be the best way ? Any Suggestion ?
Using a combination of ... , Set , .map() and .sort(). This can be done in one line.
... -> destructures array into individual items.
new Set() -> make a set out of the new items.
map() -> runs a loop and map the array into a new one.
split() -> breaks a string into array.
sort() -> sorts an array.
var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];
console.log([...new Set(array1.map(x => x.split('/')[1]))].sort());
You can easily achieve this result using Set and map
var array1 = [
"9/2022",
"7/2020",
"11/2021",
"12/2020",
"2/2020",
"4/2021",
"10/2021",
];
const result = [...new Set(array1.map((s) => s.split("/")[1]))].sort();
console.log(result);
You can map to get only the years, and then sort the results. Then you instantiate a Set which removes all duplicate entries.
new Set(['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021']
.map(dateAsString => dateAsString.split('/')[1])
.sort());
simply you can use map method to iterate over your array and then get every element and split them or separate them using Regex.
const array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];
// separate each data into month and year and get the years in the result array
const result = array1.map(data => data.split("/")[1])
// make years uinque with this simple hack
const uniqueResult = [...new Set(result)]
// now you can sort your the data
const sortedUniqueResult = uniqueResult.sort((a, b) => a - b)
console.log(sortedUniqueResult)
You can also use javascript methods chaining to convert the above snippet into one line of code:
const array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];
const result = [...new Set(array1.map(data => data.split("/")[1]).sort((a, b) => a - b))]
console.log(result)
Logic
Loop through array
Generate the year array by splitting the data on /.
Generate unique items using Array.reduce
Sort the array.
const array1 = ['9/2022', '7/2020', '11/2021', '12/2020', '2/2020', '4/2021', '10/2021'];
const result = array1.reduce((acc, curr) => {
const year = curr.split('/')[1];
if (!acc.includes(year)) {
acc.push(year);
}
return acc;
}, []).sort();
console.log(result);
First convert all of your dates to strings containing the year only. You can do this using map() on the array and the split() method on each string.
Then convert the year strings into numbers by using the parseInt() function.
Sort the year numbers using the default Array.sort() method and create a new Set (only containing unique values from the given array).
Using the spread operator (...), convert that Set into an Array.
var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];
var years = [...new Set(array1.map(date => parseInt(date.split("/")[1])).sort())];
One method is to iterate through the array split the string into month and year and return only the year: (~~ converts string into integer)
var array1 = ['9/2022','7/2020','11/2021','12/2020','2/2020','4/2021','10/2021'];
var years = [...new Set(array1.map(e => ~~e.split("/")[1]))];
console.log(years.sort())

How to merge strings from forEach loop to an array [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
Here is my code. (sorry about ugly json data)
var data = [{"ImageSets":[{"ImageSet":[{"$":{"Category":"variant"},"SwatchImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"30","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"110","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"160","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"500","$":{"Units":"pixels"}}]}],"HiResImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/81OWhCeCRtL.jpg"],"Height":[{"_":"2560","$":{"Units":"pixels"}}],"Width":[{"_":"2560","$":{"Units":"pixels"}}]}]},{"$":{"Category":"variant"},"SwatchImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"30","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"110","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"160","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"500","$":{"Units":"pixels"}}]}],"HiResImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/71e4eChoGzL.jpg"],"Height":[{"_":"2560","$":{"Units":"pixels"}}],"Width":[{"_":"2560","$":{"Units":"pixels"}}]}]},{"$":{"Category":"variant"},"SwatchImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"30","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"110","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"160","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"500","$":{"Units":"pixels"}}]}],"HiResImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/71qsXsLLRWL.jpg"],"Height":[{"_":"2560","$":{"Units":"pixels"}}],"Width":[{"_":"2560","$":{"Units":"pixels"}}]}]},{"$":{"Category":"variant"},"SwatchImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"30","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"110","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"160","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"500","$":{"Units":"pixels"}}]}],"HiResImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/91hyvNK4stL.jpg"],"Height":[{"_":"2560","$":{"Units":"pixels"}}],"Width":[{"_":"2560","$":{"Units":"pixels"}}]}]},{"$":{"Category":"variant"},"SwatchImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"30","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"110","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"160","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"500","$":{"Units":"pixels"}}]}],"HiResImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/816DOkjvgKL.jpg"],"Height":[{"_":"2560","$":{"Units":"pixels"}}],"Width":[{"_":"2560","$":{"Units":"pixels"}}]}]},{"$":{"Category":"variant"},"SwatchImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"30","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"110","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"160","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"500","$":{"Units":"pixels"}}]}],"HiResImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/819xXjN-ghL.jpg"],"Height":[{"_":"2560","$":{"Units":"pixels"}}],"Width":[{"_":"2560","$":{"Units":"pixels"}}]}]},{"$":{"Category":"primary"},"SwatchImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL30_.jpg"],"Height":[{"_":"30","$":{"Units":"pixels"}}],"Width":[{"_":"30","$":{"Units":"pixels"}}]}],"SmallImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"ThumbnailImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL75_.jpg"],"Height":[{"_":"75","$":{"Units":"pixels"}}],"Width":[{"_":"75","$":{"Units":"pixels"}}]}],"TinyImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL110_.jpg"],"Height":[{"_":"110","$":{"Units":"pixels"}}],"Width":[{"_":"110","$":{"Units":"pixels"}}]}],"MediumImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL160_.jpg"],"Height":[{"_":"160","$":{"Units":"pixels"}}],"Width":[{"_":"160","$":{"Units":"pixels"}}]}],"LargeImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL.jpg"],"Height":[{"_":"500","$":{"Units":"pixels"}}],"Width":[{"_":"500","$":{"Units":"pixels"}}]}],"HiResImage":[{"URL":["https://images-na.ssl-images-amazon.com/images/I/71-w1EWOaWL.jpg"],"Height":[{"_":"2560","$":{"Units":"pixels"}}],"Width":[{"_":"2560","$":{"Units":"pixels"}}]}]}]}]}]
data[0].ImageSets[0].ImageSet.forEach((image) => {
var images = image.LargeImage[0].URL[0]
console.log(images)
})
I'm getting all URL's in console log.
I tried to merge them with split() but it's not working. Each string is getting its own array. Similarly if i use var images = image.LargeImage[0].URL. Each string return it's own array.
What i want is a single array with all URL's like this.
["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL.jpg", "https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL.jpg", "https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL.jpg", "https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL.jpg", "https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL.jpg", "https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL.jpg","https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL.jpg"]
Thanks.
It makes more sense here to just use .map(), as it will create a new Array with the return value of each iteration
var data=[{ImageSets:[{ImageSet:[{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/81OWhCeCRtL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/71e4eChoGzL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/71qsXsLLRWL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/91hyvNK4stL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/816DOkjvgKL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/819xXjN-ghL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"primary"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/71-w1EWOaWL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]}]}]}];
let urlsArray = data[0].ImageSets[0].ImageSet.map((image) => {
return image.LargeImage[0].URL[0];
});
console.log(urlsArray);
This is how you would do it with .forEach(), additionally
var data=[{ImageSets:[{ImageSet:[{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41NeQS3RMzL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/81OWhCeCRtL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/316W1tHrMuL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/71e4eChoGzL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/31TgrPVXYjL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/71qsXsLLRWL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/51qcfjgaAZL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/91hyvNK4stL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41numbb3sgL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/816DOkjvgKL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"variant"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41PLx0E2sqL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/819xXjN-ghL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]},{$:{Category:"primary"},SwatchImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL30_.jpg"],Height:[{_:"30",$:{Units:"pixels"}}],Width:[{_:"30",$:{Units:"pixels"}}]}],SmallImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],ThumbnailImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL75_.jpg"],Height:[{_:"75",$:{Units:"pixels"}}],Width:[{_:"75",$:{Units:"pixels"}}]}],TinyImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL110_.jpg"],Height:[{_:"110",$:{Units:"pixels"}}],Width:[{_:"110",$:{Units:"pixels"}}]}],MediumImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL._SL160_.jpg"],Height:[{_:"160",$:{Units:"pixels"}}],Width:[{_:"160",$:{Units:"pixels"}}]}],LargeImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/41bz4b5IbLL.jpg"],Height:[{_:"500",$:{Units:"pixels"}}],Width:[{_:"500",$:{Units:"pixels"}}]}],HiResImage:[{URL:["https://images-na.ssl-images-amazon.com/images/I/71-w1EWOaWL.jpg"],Height:[{_:"2560",$:{Units:"pixels"}}],Width:[{_:"2560",$:{Units:"pixels"}}]}]}]}]}];
let urlsArray = [];
data[0].ImageSets[0].ImageSet.map((image) => {
urlsArray.push(image.LargeImage[0].URL[0]);
});
console.log(urlsArray);

Unable store the objects into an array in javascript [duplicate]

This question already has answers here:
Push Object in Array [duplicate]
(2 answers)
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 3 years ago.
I'm trying to store the objects into Javascript array.
var res=[
{name:'Alex',place:'US',age:20},
{name:'Jason',place:'Canada',age:25}
];
var obj={};
var data=[];
for ( let i in res){
obj.name=res[i].name;
obj.place=res[i].place;
data.push(obj);
}
console.log(data);
My expected output:
[
{name:'Alex',place:'US'}.
{name:'Jason',place:'Canada'}
]
Actual output I got:
[
{name:'Jason',place:'Canada'},
{name:'Jason',place:'Canada' }
]
Why I'm getting this type of output?
I'm noobie.Please help me.
You only have one object in memory with your var obj={}; - that line runs once, and creates one object in memory, which you then proceed to mutate on every iteration in the for loop, and push to the array. At the end, the array contains 2 references to the same object.
Create the object inside the loop instead:
for ( let i in res){
var obj = {};
obj.name=res[i].name;
obj.place=res[i].place;
data.push(obj);
}
You could also consider using .map instead:
const res=[
{name:'Alex',place:'US',age:20},
{name:'Jason',place:'Canada',age:25}
];
const data = res.map(({ name, place }) => ({ name, place }));
console.log(data);
You are basically updating the same object again and again in your loop. You need to have different objects for each loop. Hence, you need to move var obj={} inside the for loop.
var res=[{name:'Alex',place:'US',age:20},{name:'Jason',place:'Canada',age:25}];
var data=[];
for ( let i in res){
var obj={};
obj.name=res[i].name;
obj.place=res[i].place;
data.push(obj);
}
console.log(data);
Using .map would be much easier
var res=[
{name:'Alex',place:'US',age:20},
{name:'Jason',place:'Canada',age:25}
];
var obj={};
var data = res.map(({ name, place }) => {
return ({ name, place })
});
console.log(data);

Categories

Resources