简体   繁体   English

JavaScript:如何通过嵌套在 object 2 层深的值过滤数组

[英]JavaScript: How to filter array by values nested in object 2 level deep

I have an array of objects.我有一组对象。

Within each object of the array, there can be multiple "Book" objects, all with dynamic keys.在数组的每个 object 中,可以有多个“Book”对象,都带有动态键。 I want the objects with at least one "Book" object that is new.我想要至少有一本新的“书”object 的对象。

For example:例如:

const arr = [
  {
    id: '123',
    book1242: {isNew: true},
    book9023: {isNew: false},
  },
  {
    id: '123',
    book0374: {isNew: false},
    book9423: {isNew: false},
  },
  {
    id: '123',
    book8423: {isNew: false},
    book9023: {isNew: false},
  },
  {
    id: '123',
    book6534: {isNew: true},
    book9313: {isNew: false},
  },
]

So my filtered array will consist of the first and last element of the original array所以我过滤后的数组将由原始数组的第一个和最后一个元素组成

Expected filtered array预期过滤数组

const arr = [
  {
    id: '123',
    book1242: {isNew: true},
    book9023: {isNew: false},
  },
  {
    id: '123',
    book6534: {isNew: true},
    book9313: {isNew: false},
  },
]

I have tried using filter and map , but I get to the point where I have to loop through and check which book is new and I'm not sure how to return that object within the filter.我试过使用filtermap ,但我到了必须循环检查哪本书是新书的地步,我不确定如何在过滤器中返回 object 。

It sounds like you want something like this:听起来你想要这样的东西:

arr.filter((o) => Object.values(o).some((b) => b.isNew))

Will your array only ever have keys that are id and bookwxyz ?您的数组只会有idbookwxyz键吗? If not you may need to do some checking on the values to make sure they aren't undefined如果不是,您可能需要对值进行一些检查以确保它们不是未定义的

You could also explicitly check the key using Object.entries and a regular expression:您还可以使用 Object.entries 和正则表达式显式检查密钥:

arr.filter((o) => Object.entries(o).some(([key, value]) => /book\d{4}/.test(key) && value.isNew))

You can use Array#some with Object.values .您可以将Array#someObject.values一起使用。

 const arr = [ { id: '123', book1242: {isNew: true}, book9023: {isNew: false}, }, { id: '123', book0374: {isNew: false}, book9423: {isNew: false}, }, { id: '123', book8423: {isNew: false}, book9023: {isNew: false}, }, { id: '123', book6534: {isNew: true}, book9313: {isNew: false}, }, ]; const res = arr.filter(obj=>Object.values(obj).some(({isNew})=>isNew)); console.log(res);

 const arr=[{id:"123",book1242:{isNew:,0}:book9023:{isNew,:1}},{id:"123":book0374,{isNew::1},book9423:{isNew,:1}}:{id,"123":book8423:{isNew,:1},book9023:{isNew:,1}}:{id:"123";book6534.{isNew,.0}.book9313,{isNew..1}}]; const res = arr.filter(obj => { for(const [key, val] of Object.entries(obj)){ if(key.substring(0,4) === 'book' && val.isNew === true){ return obj } } }) console.log(res)

 let sorted =[]
  arr.forEach((el, i) => {
         let obj_values = Object.values(el);
         obj_values = obj_values.filter((elem) => typeof elem == 'object' && elem.isNew == true);
if(obj_values.length) sorted.push(arr[i])
     })

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM