[英]How to identify type of an javascript object field which is a Array<{}>
I have JavaScript object which comes from a request which may include nested fields, I need to identify field which is a type of Array<{}> and i need to iterate that field alone, but in javascript i could not able to achieve that.我有 JavaScript object 来自可能包含嵌套字段的请求,我需要识别字段类型为 Array<{}> 并且我需要单独迭代该字段,但在 ZDE9B9ED78D7E2E19DCEEFFEE780E2 中无法实现。 anyone please suggest me a solution to find the field based on field type.
任何人都请建议我根据字段类型查找字段的解决方案。
sample object样品 object
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}
}
Among these fields, I need to iterate the field which is an Array<{}>(ie interested_books)在这些字段中,我需要迭代 Array<{}> 的字段(即interested_books)
Use isArray to check if value is an array使用isArray检查 value 是否为数组
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 } } for (let keys in obj) { if (Array.isArray(obj[keys])) { obj[keys].forEach(item => console.log(item)) } }
Use Object.entires()
and isArray
to check if it's an array使用
Object.entires()
和isArray
来检查它是否是一个数组
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 }, }; function getArrWithOb(obj) { return Object.fromEntries( Object.entries(obj).filter((o) => { if (Array.isArray(o[1])) { if (o[1].every((ob) => typeof ob == "object")) { return o; } } }) ); } console.log(getArrWithOb(obj));
From the comments it looks like what you want to be returned is the name of the property You could use reduce
从评论看来,您想要返回的是属性的名称您可以使用
reduce
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 }, }; function getArrWithOb(obj) { return Object.entries(obj).reduce((r, o) => { if (Array.isArray(o[1])) { if (o[1].every((ob) => typeof ob == "object")) { r.push(o[0]); } } return r; }, []); } console.log(getArrWithOb(obj));
You can also use instanceof
您也可以使用
instanceof
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} } for (const v in obj) obj[v] instanceof Array && console.log(obj[v]);
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 } } for (const key in obj) { if (obj[key] instanceof Array) { console.log('I am array', key); const isArrayOfObject = obj[key].every(item => typeof item === "object"); if (isArrayOfObject) { // your_logic_here } } }
Try the function below.试试下面的 function。 This is to get the items in the object that are arrays.
这是为了获取 object 中的项目,即 arrays。
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 }
};
const getArraysFromObject = obj => {
const results = {};
Object.keys(obj).map(field => {
if (Array.isArray(obj[field])) {
results[field] = obj[field];
}
});
return results;
};
console.log(results);
// {
// "interested_books": [
// { "book_name": "xxx", "author_name": "yyy" },
// { "book_name": "aaa", "author_name": "zzz" }
// ],
// "hobbies": ["reading", "football"]
// }
You can use typeof to check if value is an array您可以使用typeof检查 value 是否为数组
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} } for (x in obj) { txt = obj[x]; if(typeof(txt) == "object") { for(oj in txt) { ojval = txt[oj]; console.log(ojval); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.