[英]In javascript how can we identify whether an object is a Hash or an Array?
[英]How to identify type of an javascript object field which is a Array<{}>
我有 JavaScript object 來自可能包含嵌套字段的請求,我需要識別字段類型為 Array<{}> 並且我需要單獨迭代該字段,但在 ZDE9B9ED78D7E2E19DCEEFFEE780E2 中無法實現。 任何人都請建議我根據字段類型查找字段的解決方案。
樣品 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}
}
在這些字段中,我需要迭代 Array<{}> 的字段(即interested_books)
使用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)) } }
使用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));
從評論看來,您想要返回的是屬性的名稱您可以使用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));
您也可以使用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 } } }
試試下面的 function。 這是為了獲取 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"]
// }
您可以使用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.