I am a beginner in JavaScript and I am currently working on an exercise. My goal is to iterate through an array of objects within another object, map the index values from one object to the id values in another object, and based on that, perform a certain operation. Below is my object:
obj= {
"text": [{
"id": 0,
"name": "aaa"
},{
"id": 1,
"name": "bbb"
},{
"id": 3,
"name": "ccc"
},{
"id": 4,
"name": "ddd"
}],
"content": [
{
"id": 123,
"index": 0
},{
"id": 1232,
"index": 2
},{
"id": 12333,
"index": 3
}
]
}
This section has two parts:
1) To display the values of the name
property within the text
object as follows:
aaa
bbb
ccc
ddd
eee
fff
2) I aim to insert a -
after every group of name values based on the index provided within the content
object. Essentially, I need to match the index values from the content object to the id values inside the text object and add a -
accordingly. In this case, with indexes of 0, 2, and 3, the output should appear like:
aaa
bbb
-
ccc
-
ddd
Here is the code I have for part 1 so far:
function string() {
var arr = obj.text;
var output = '';
for(var i=0; i<arr.length; i++) {
output += '<div>'+ arr[i].text + '</div>';
}
document.getElementById("container").innerHTML = output;
}
I would appreciate any help regarding part 2. Do you have any suggestions?