如何识别数组 <{}> 的 javascript 对象字段的类型

我有一个 JavaScript 对象,它来自一个可能包含嵌套字段的请求,我需要识别属于 Array<{}> 类型的字段,我需要单独迭代该字段,但在 javascript 中我无法实现这一点。任何人都请建议我根据字段类型查找字段的解决方案。


样本对象


const obj = {

name:'jane', 

age: 22,

interested_books:[{book_name:'xxx', author_name:'yyy'}, {book_name:'aaa', author_name:'zzz'}],

hobbies:['reading', 'football'],

address:{street_name:'dace street', door_no:12}

}

在这些字段中,我需要迭代一个数组<{}>的字段(即interested_books)


慕后森
浏览 156回答 4
4回答

翻翻过去那场雪

使用 Object.entires()andisArray检查它是否是一个数组obj = {&nbsp; name: "jane",&nbsp; age: 22,&nbsp; interested_books: [&nbsp; &nbsp; { book_name: "xxx", author_name: "yyy" },&nbsp; &nbsp; { book_name: "aaa", author_name: "zzz" },&nbsp; ],&nbsp; hobbies: ["reading", "football"],&nbsp; address: { street_name: "dace street", door_no: 12 },};function getArrWithOb(obj) {&nbsp; return Object.fromEntries(Object.entries(obj).filter((o) => {&nbsp; if (Array.isArray(o[1])) {&nbsp; &nbsp; if (o[1].every((ob) => typeof ob == "object")) {&nbsp; &nbsp; &nbsp; return o;&nbsp; &nbsp; }&nbsp; }})&nbsp; );}console.log(getArrWithOb(obj));从评论看来你想要返回的是你可以使用的属性的名称reduceobj = { name: "jane", age: 22, interested_books: [ { book_name: "xxx", author_name: "yyy" }, { book_name: "aaa", author_name: "zzz" }, ], hobbies: ["reading", "football"], address: { street_name: "dace street", door_no: 12 }, };function getArrWithOb(obj) {&nbsp; return Object.entries(obj).reduce((r, o) => {&nbsp; &nbsp; if (Array.isArray(o[1])) {&nbsp; &nbsp; &nbsp; if (o[1].every((ob) => typeof ob == "object")) {&nbsp; &nbsp; &nbsp; &nbsp; r.push(o[0]);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return r;&nbsp; }, []);}console.log(getArrWithOb(obj));

长风秋雁

使用isArray检查值是否为数组const obj = {&nbsp; name: 'jane',&nbsp; age: 22,&nbsp; interested_books: [{&nbsp; &nbsp; book_name: 'xxx',&nbsp; &nbsp; author_name: 'yyy'&nbsp; }, {&nbsp; &nbsp; book_name: 'aaa',&nbsp; &nbsp; author_name: 'zzz'&nbsp; }],&nbsp; hobbies: ['reading', 'football'],&nbsp; address: {&nbsp; &nbsp; street_name: 'dace street',&nbsp; &nbsp; door_no: 12&nbsp; }}for (let keys in obj) {&nbsp; if (Array.isArray(obj[keys])) {&nbsp; &nbsp; obj[keys].forEach(item => console.log(item))&nbsp; }}

幕布斯6054654

const obj = {&nbsp; name: 'jane',&nbsp; age: 22,&nbsp; interested_books: [{&nbsp; &nbsp; book_name: 'xxx',&nbsp; &nbsp; author_name: 'yyy'&nbsp; }, {&nbsp; &nbsp; book_name: 'aaa',&nbsp; &nbsp; author_name: 'zzz'&nbsp; }],&nbsp; hobbies: ['reading', 'football'],&nbsp; address: {&nbsp; &nbsp; street_name: 'dace street',&nbsp; &nbsp; door_no: 12&nbsp; }}for (const key in obj) {&nbsp; if (obj[key] instanceof Array) {&nbsp; &nbsp; console.log('I am array', key);&nbsp; &nbsp; const isArrayOfObject = obj[key].every(item => typeof item === "object");&nbsp; &nbsp; if (isArrayOfObject) {&nbsp; &nbsp; &nbsp; // your_logic_here&nbsp; &nbsp; }&nbsp; }}

交互式爱情

试试下面的功能。这是为了获取对象中的数组项。const obj = {&nbsp; name: 'jane',&nbsp; age: 22,&nbsp; interested_books: [{ book_name: 'xxx', author_name: 'yyy' }, { book_name: 'aaa', author_name: 'zzz' }],&nbsp; hobbies: ['reading', 'football'],&nbsp; address: { street_name: 'dace street', door_no: 12 }};const getArraysFromObject = obj => {&nbsp; const results = {};&nbsp; Object.keys(obj).map(field => {&nbsp; &nbsp; if (Array.isArray(obj[field])) {&nbsp; &nbsp; &nbsp; results[field] = obj[field];&nbsp; &nbsp; }&nbsp; });&nbsp; return results;};console.log(results);&nbsp;// {//&nbsp; &nbsp;"interested_books": [//&nbsp; &nbsp; &nbsp;{ "book_name": "xxx", "author_name": "yyy" },//&nbsp; &nbsp; &nbsp;{ "book_name": "aaa", "author_name": "zzz" }//&nbsp; &nbsp;],//&nbsp; &nbsp;"hobbies": ["reading", "football"]// }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript