как получить номер индекса для каждого объекта в массиве. У меня JSON с несколькими вложенными массивами объектов, и мне нужно установить номер индекса ключа POSITION.
Я получаю это JSON динамически, и мне нужно l oop через JSON, и на каждом POSITION ключе необходимо установить indexOF()
число, чтобы получить позицию объекта внутри массив.
Моя проблема в том, как найти все ключи с именем POSITION
и установить indexOf()
число для значения?
Я знаю, и я пытаюсь получить его в l oop для каждого вложенного массива, но есть ли ЛУЧШИЙ способ? Более быстрый путь?
Вот пример с вложенным for loops
Thnx
Вот JSON
{
"id": 175,
"name": "dag",
"title": "dfgd",
"page": [{
"id": 551,
"name": "page1",
"title": "Page 1",
"position": 0, ---> here I need set index number for obj in this page array
"questions": [{
"id": 70,
"name": "text1",
"text": "Questitexton 1",
"position": 0, ---> here I need set index number for obj in this questions array
"data": [{
"id": 56,
"name": "data",
"position": 0, ---> here I need set index number for obj in this data array
"text": "Control 3"
}]
}, {
"id": 69,
"name": "data3",
"text": "data 3",
"position": 1, ---> here I need set index number for obj in this array
"data": [{
"id": 55,
"name": "data2",
"position": 0, ---> here I need set index number for obj in this data array
"text": "Control 3"
}]
}]
}, {
"id": 552,
"name": "page2",
"title": "Page 2",
"position": 1, ---> here I need set index number for obj in this page array
"questions": [{
"id": 73,
"name": "text1",
"text": "Questitexton 1",
"position": 0, ---> here I need set index number for obj in this questions array
"data": [{
"id": 75,
"name": "data",
"position": 0, ---> here I need set index number for obj in this data array
"text": "Control 3"
}]
}, {
"id": 45,
"name": "data3",
"text": "data 3",
"position": 1, ---> here I need set index number for obj in this questions array
"data": [{
"id": 798,
"name": "data2",
"position": 0, ---> here I need set index number for obj in this data array
"text": "Control 3"
}]
}]
}]
}
Моя текущая попытка
const animals = [{
id: 175,
name: "dag",
title: "dfgd",
page: [{
id: 551,
name: "page1",
title: "Page 1",
position: null,
questions: [{
id: 70,
name: "text1",
text: "Questitexton 1",
position: null,
data: [{
id: 56,
name: "data",
position: null,
text: "Control 3"
}]
},
{
id: 69,
name: "data3",
text: "data 3",
position: null,
data: [{
id: 55,
name: "data2",
position: null,
text: "Control 3"
}]
}
]
},
{
id: 552,
name: "page2",
title: "Page 2",
position: null,
questions: [{
id: 73,
name: "text2",
text: "Questitexton 1",
position: null,
data: [{
id: 75,
name: "data",
position: null,
text: "Control 3"
}]
},
{
id: 45,
name: "data5",
text: "data 3",
position: null,
data: [{
id: 798,
name: "data2",
position: null,
text: "Control 3"
}]
}
]
}
]
}];
for (let data of animals) {
for (let data1 of data.page) {
data1.position = data.page.indexOf(data1);
for (let data2 of data1.questions) {
data2.position = data1.questions.indexOf(data2);
for (let data3 of data2.data) {
data3.position = data2.data.indexOf(data3);
}
}
}
}
console.log(animals);