How to append JSON object to JSON object? [duplicate] - javascript

This question already has an answer here:
Dynamic object property names?
(1 answer)
Closed 8 months ago.
so i have this JSON file:
{"fjord-200-200.jpg":[]}
I read it using
let data = fs.readFileSync(cachedName);
let cachedInJSON: JSON = JSON.parse(data.toString());
so i have now JSON object, that i want to append to this new JSON object
let newData = {
fileNameFormatted: []
}
when i tried using
let newJson = {...cachedInJSON, ...newData}
Result was this:
{"fjord-200-200.jpg":[],"fileNameFormatted":[]}
Wanted result that i want to achieve is:
{"fjord-200-200.jpg":[],"fjord-300-300.jpg":[]}
fileNameFormatted is a variable holding fjord-300-300.jpg

let newData = {
[fileNameFormatted]: [] // Notice the brackets around the key
}

Related

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);

How can I get a key from an object by its value? [duplicate]

This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 3 years ago.
I am working in react. I have a constant like
const priorityMap = {
"medium":"clean",
"high":"breaker",
"low":"promote"
}
If I do the following , I am getting result.
const map1 = priorityMap["medium"];
console.log("print checkgroup " + map1)
But I want to do the reverse thing. My input is "clean" and I want to retrieve "medium". Is there any way to fetch the key?
It would be :
const valueToSearch = "clean"
const result = Object.entries(priorityMap).find(entry => entry[1] === valueToSearch)[1]

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);

Add multiple options in array with javascript [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 7 years ago.
I have this var
var allwords = {};
And i need push more options in this array like:
allwords.push = {"Ctrl-Space": "autocomplete"};
allwords.push = {"Ctrl-pause": "closewindow"};
And look like this:
allwords = {"Ctrl-Space": "autocomplete", "Ctrl-pause": "closewindow"};
How can't i do?
push is for Array objects. For traditional objects, assign the properties manually like
var allwords = {};
allwords["Ctrl-Space"] = "autocomplete";
allwords["Ctrl-pause"] = "closewindow";
console.log(allwords);

javascript convert array to json object [duplicate]

This question already has answers here:
JavaScript: Converting Array to Object
(2 answers)
Closed 8 years ago.
I've got the following array:
array = [{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}]
i wanto convert it to json like this:
json = {{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}}
So than i can store everything in mongodb in a unique document.
Regards,
Just run a loop and equate...like...
var obj = {};
for(var i=0; i<array.length; i++)
{
obj[i] = array[i]
}
It will do
{
0:{"id":144,"price":12500000},
1:{"id":145,"price":13500000},
2:{"id":146,"price":13450000},
3:{"id":147,"price":11500000},
4:{"id":148,"price":15560000}
}
Because your JSON is invalid.
Not sure what you are asking
From array or another variable to json string =>
var str = JSON.stringify(thing);
From a json string to variable
var thing = JSON.parse(str);

Categories

Resources