简体   繁体   English

如何使用构造函数Javascript在嵌套数组中的键值对中输出键

[英]how to output keys in a key value pair within nested arrays using constructor Javascript

Within the array of 'items' I wanted to output within the 'info' array only the keys: [ 'stringTwo', 'StringThree' ] and also output the value String Three 在“项目”数组中,我想在“信息”数组中仅输出键: [ 'stringTwo', 'StringThree' ] ,还输出值String Three

 let items = [ { string: 'string1', info: { stringTwo:'String Two', stringThree: 'String Three' }, size:3445 }, { string: 'string2', info: 'ruby files' }, { string: 'string3', info: '' }, { string: 'string4 without info key', } ]; 

I tried with both of these codes: 我尝试了这两个代码:

 data.forEach((data) => { if(data.info.constructor === Object) { console.log(Object.keys(data.info)); } }) data.forEach((data) => { if(data.info.constructor === Object) { console.log((data.info.stringThree)); } }) 

the first one should output the Keys [ 'stringTwo', 'StringThree' ] and the second one should output String Three 第一个应该输出Keys [ 'stringTwo', 'StringThree' ] ,第二个应该输出String Three

I'm wondering why in a more larger scale array that has more key-value pairs both doesn't work and gives me an input of TypeError: Cannot read property 'constructor' of undefined ? 我想知道为什么在具有更多键值对的更大比例数组中既不起作用,又给我输入TypeError: Cannot read property 'constructor' of undefined吗? if so are there other ways without using constructor? 如果是这样,还有其他方法不使用构造函数吗?

Your larger scale array probably don't have any info key. 您的大型阵列可能没有任何info键。 To prevent having an error, you should : 为避免出现错误,您应该:

  • Change your items array to put an info key, even empty 更改项目数组以放置info键,即使是空的

    or 要么

  • Add a typeof data.info !== "undefined" condition to check if the info key is defined on each item before trying to access it. 添加 typeof data.info !== "undefined" 条件,以在尝试访问每个项目之前检查是否在每个项目上定义info键。

Here is a working example : 这是一个工作示例:

 let items = [ { string: 'string1', info: { stringTwo:'String Two', stringThree: 'String Three' }, size:3445 }, { string: 'string2', info: 'ruby files' }, { string: 'string3', info: '' }, { string: 'string4 without info key', } ]; items.forEach((data) => { if(typeof data.info !== "undefined" && data.info.constructor === Object) { console.log(data.info.stringThree); } }) 

暂无
暂无

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

相关问题 如何在嵌套数组中的键值对中输出键 - How to output keys in key value pairs in nested arrays JavaScript 如何使用JavaScript中的两个arrays创建键值对? - How to create key value pair using two arrays in JavaScript? 使用 javaScript 将键从键值对转换为大写 - Convert keys from key value pair to capital case using javaScript 在Javascript中,如何检查嵌套在另一个键/值对中的特定键/值对的存在? - In Javascript, how can I check the existence of a specific key/value pair nested inside another key/value pair? 如何在 javascript 中以键值对格式制作嵌套数组对象 - how to make nested array objects in javascript in a key value pair format 如何在JavaScript中准备嵌套的键值对列表 - How can i prepare nested key value pair list in JavaScript 如何在字符串中的Javascript中访问已解析的JSON中的嵌套键值对 - How to access nested key value pair in parsed JSON in Javascript in string 如何在 Javascript 的键值对中转换嵌套的 Object - How to Convert Nested Object in Key Value Pair in Javascript 如何更新 Javascript 中嵌套对象数组中的键/值对 - How to update a key/value pair in a nested array of objects in Javascript 在父对象和子数组的嵌套数据结构中,如何通过特定(唯一)键值对找到数据项? - How does one, within a nested data-structure of parent-objects and child-arrays, find a data-item by a specific (unique) key-value pair?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM