0

I want to create a javascript object such as below :

{
 {
 "Image" : img_src,
 "Text" : text
 },
}

I have two arrays

img_src = [ value1, value2, value3 ....... ]
text = [ text1, text2, text3 .......]

I want to map the values of these arrays in that object such that values of img_src is placed next to key "Image" and values of text is places next to key "Text" in the following manner:

{
 {
 "Image" : value1,
 "Text" : text1
 },
 {
 "Image" : value2,
 "Text" : text2
 },
 {
 "Image" : value3,
 "Text" : text3
 } 
}

and so on. I tried reading javascript documentation and tried all sort of things but I am unable to come up with a solution. Could someone kindly explain how can I achieve this?

pilchard
13.1k5 gold badges14 silver badges28 bronze badges
asked Apr 26, 2023 at 20:30
2

2 Answers 2

0

Either use a for loop to iterate over both arrays and construct an object from them or use the .map() function.

answered Apr 26, 2023 at 20:38
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you want an array of objects as the output. If so, you can map over one of the arrays, ex. img_src, and use a combination of items and indexes to generate each combined object.

let combined = img_src.map((img, index) => ({Image: img, Text: text[index]}));
answered Apr 26, 2023 at 20:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.