var data = {
content: [{
api: 'menu',
content: {
elem: 'item'
}
}]
};
var apiData = {
menu: ['Home', 'Content', 'About']
};
test = function (json, api) {
if(Array.isArray(json)) {
json.forEach(function (element) {
if(typeof element.content === 'object') {
var content = [];
api[element.api].forEach(function (apiElement, n) {
content[n] = element.content;
content[n].content = apiElement;
});
console.log(content);
}
if (typeof element.content !== 'string') {
test(element.content, api);
} else {
test(element, api);
}
});
} else {
if(typeof json !== 'string') {
test(json.content, api);
}
}
};
test(data, apiData);
Текущий вывод:
[ { elem: 'item', content: 'About' },
{ elem: 'item', content: 'About' },
{ elem: 'item', content: 'About' } ]
Нужно вывести:
[ { elem: 'item', content: 'Home' },
{ elem: 'item', content: 'Content' },
{ elem: 'item', content: 'About' } ]