简体   繁体   English

Reach 数组中 object 中的元素是数组中 object 中的元素

[英]Reach element in an object inside an array which is an element in an object inside an array

I've tried to reach an element in object inside an array which is an element in an object inside an array.我试图到达数组内 object 中的一个元素,该数组是数组内 object 中的一个元素。 Look at the code under.看下面的代码。 The thirdElementInObject has variable length. thirdElementInObject 具有可变长度。 My plan is to make list element with every object in the mainArray.我的计划是用 mainArray 中的每个 object 制作列表元素。 I haven't made it with the elements in the thirdElementInObject array.我还没有用 thirdElementInObject 数组中的元素实现它。 Does anyone know how I can do this?有谁知道我该怎么做?

mainArray = [
   {firstElementInObject: xyz,
   secondElementInObject: abc,
   thirdElementInObject: [
     {name: zzz,
     age: aaa,
     },
     {name: cde,
     age: 123,
     },
     ],
},
   {firstElementInObject: xyz,
   secondElementInObject: abc,
   thirdElementInObject: [
     {name: xxx,
     age: yyy,
     },
     {name: abc,
     age: def,
     },
     {name: abc,
     age: def,
     }
     ],
}
]

const parentElementDash = document.querySelector('.mainArrayUl');

const updateHTML = function () {  
    if (mainArray.length > 0) {
        let result = mainArray.map(element => {
            return `
                <li>
                    <p>${element.firstElementInObject}</p>
                    <p>${element.secondElementInObject}</p>

                    /* Here I want to write the name in the 
                   thirdElementInObject. */

                </li>`  
        });
        parentElementDash.innerHTML = result.join('');
    }
}


updateHTML();
  • You can get all the names as an array using .map() .您可以使用.map()将所有名称作为数组获取。
  • You can then use .join() to convert the array to string and insert it in the HTML.然后,您可以使用.join()将数组转换为字符串并将其插入到 HTML 中。

Try this尝试这个

 var mainArray = [ { firstElementInObject: 'xyz', secondElementInObject: 'abc', thirdElementInObject: [ {name: 'zzz', age: 'aaa',}, {name: 'cde', age: 123,}, ], }, { firstElementInObject: 'xyz', secondElementInObject: 'abc', thirdElementInObject: [ {name: 'xxx', age: 'yyy',}, {name: 'abc', age: 'def',}, {name: 'abc', age: 'def',} ], } ] const parentElementDash = document.querySelector('.mainArrayUl'); const updateHTML = function () { if (mainArray.length > 0) { let result = mainArray.map(element => { return ` <li> <p>${element.firstElementInObject}</p> <p>${element.secondElementInObject}</p> <p>${element.thirdElementInObject.map(({name}) => name).join(', ')}</p> </li>` }); parentElementDash.innerHTML = result.join(''); } } updateHTML();
 <ul class="mainArrayUl"></ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取 object 内部数组中元素的键 - get key of element in array which is inside object 返回对象数组内部的数组元素 - Return Array Element inside of an Object array 如何从对象内部的数组中删除特定元素? - How to remove a specific element from an array which is inside of an object? 如何从ImmutableJS中的对象内部的数组中删除元素 - How to delete an element from an array which is inside an object in ImmutableJS 访问对象内部多维数组内的对象元素 - Accessing an element of an object inside a multidimensional array inside an object 在数组内查找数组中的元素并返回带有父对象的元素 - Find element in array inside array and return element with parent object 如何比较两个不同对象内的 arrays 的元素并显示哪个元素属于 object 内的哪个数组 - how to compare elements of arrays inside two different objects and display which element belonged to which array that was inside the object 如何将元素推入 Mongoose 中另一个数组中的 object 中的数组? - How to push an element into an array inside an object inside another array in Mongoose? 访问JSON对象中的数组元素 - Accessing an array element inside JSON Object 从数组中删除一个元素,即在 Object 内 - Deleting an element from an Array, that is inside an Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM