为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕后森 2023-01-06 16:20:03
我有一个 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)
查看完整描述

4 回答

?
翻翻过去那场雪

TA贡献2065条经验 获得超14个赞

使用 Object.entires()andisArray检查它是否是一个数组


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));


查看完整回答
反对 回复 2023-01-06
?
长风秋雁

TA贡献1757条经验 获得超7个赞

使用isArray检查值是否为数组


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))

  }

}


查看完整回答
反对 回复 2023-01-06
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

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

    }

  }

}


查看完整回答
反对 回复 2023-01-06
?
交互式爱情

TA贡献1712条经验 获得超3个赞

试试下面的功能。这是为了获取对象中的数组项。


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"]

// }


查看完整回答
反对 回复 2023-01-06
  • 4 回答
  • 0 关注
  • 155 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信