从JavaScript中的Object.entries中的数组破坏 [英] Array destructing from Object.entries in Javascript

查看:137
本文介绍了从JavaScript中的Object.entries中的数组破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是代码:



  const posts = [{数据:{id:1,date:2019-02-03,ev_filter_1:[art,foodie],ev_filter_2:[value1,value2],ev_filter_3:[value1,value2 ],ev_filter_4:[all,12+]}},{data:{id:2,date:,ev_filter_1:[arti,foodie],ev_filter_2:[value1 value2],ev_filter_3:[value1,value2],ev_filter_4:[all,19+]}},{data:{id:3,date:2019-02-03 ev_filter_1:[art,food,ev_filter_2:[value1,value75],ev_filter_3:[value1,value2],ev_filter_4:[all,12+]}} ];函数sift2(arrObjLit,pattern,... values){const toMatch = new Set(values)const result = arrObjLit.map(o => o.data).filter(o => Object.entries(o).filter(([k,v])=> {console.log(`$ {k}:$ {v}`)return true}).filter(([k,v])=> k.startsWith(pattern)).filter(([k,v] )=> Array.isArray(v)).filter(([k,v])=> toMatch.has(v)).length> 0)return result;} console.log(... sift2(posts,ev_,value75,12 +));  



这让我感到困惑基于此帖
我期望在过滤器中的数组破坏是错误的。但是,不是。这正是我正在寻找的。为什么破解在过滤器方法中是平的?我观察到错误吗?

  .filter(o => 
Object.entries(o)
.filter(([k,v])=> k.startsWith(pattern))


条目的输出上调用第二个过滤器 code>。请注意,您正在使用

  .filter(o => Object.entries(o).filter (...)... 

而不是

  .map(Object.entries).filter(o => o。...)






表示,我会将您的功能重写为

  function sift2(arrObjLit,pattern,... values){
const toMatch = new Set(values)
return arrObjLit.filter(o =>
Object.entries .data)
.some(([k,v])=> {
console.log(`$ {k}:$ {v}`);
return k.startsWith (图案)
&&& Array.isArray(v)
&& v.some(x => toMatch.has(x));
})
);
}


Here is the code in question:

const posts = [{
  data: {
    id: 1,
    date: "2019-02-03",
    ev_filter_1: ["art", "foodie"],
    ev_filter_2: ["value1", "value2"],
    ev_filter_3: ["value1", "value2"],
    ev_filter_4: ["all", "12+"]
  }
},
  {
    data: {
      id: 2,
      date: "",
      ev_filter_1: ["arti", "foodie"],
      ev_filter_2: ["value1", "value2"],
      ev_filter_3: ["value1", "value2"],
      ev_filter_4: ["all", "19+"]
    }
  },
  {
    data: {
      id: 3,
      date: "2019-02-03",
      ev_filter_1: ["art", "foodie"],
      ev_filter_2: ["value1", "value75"],
      ev_filter_3: ["value1", "value2"],
      ev_filter_4: ["all", "12+"]
    }
  }
];

function sift2(arrObjLit, pattern, ...values) {
  const toMatch = new Set(values)
  const result = arrObjLit.map(o => o.data)
  .filter(o =>
      Object.entries(o)
      .filter(([k, v]) => {
        console.log(`${k}: ${v}`)
        return true
      })
      .filter(([k, v]) => k.startsWith(pattern))
      .filter(([k, v]) => Array.isArray(v))
      .filter(([k, v]) => toMatch.has(v))
          .length > 0
  )
  return result;
}

console.log(...sift2(posts, "ev_", "value75", "12+"));

Which is baffling me. Based on this post I would expect the array destructing in filter to be wrong. And yet, it's not. It's exactly what I am looking for. Why would the destructing be flat in the filter method? Am I observing things wrong?

.filter(o =>
      Object.entries(o)
      .filter(([k, v]) => k.startsWith(pattern))

解决方案

Because you are now properly nesting the iterations, calling the second filter directly on the output of entries. Notice that you are using

.filter(o => Object.entries(o).filter(…).… )

instead of

.map(Object.entries).filter(o => o.… )


That said, I'd rewrite your function to

function sift2(arrObjLit, pattern, ...values) {
  const toMatch = new Set(values)
  return arrObjLit.filter(o =>
    Object.entries(o.data)
    .some(([k, v]) => {
      console.log(`${k}: ${v}`);
      return k.startsWith(pattern)
          && Array.isArray(v)
          && v.some(x => toMatch.has(x));
    })
  );
}

这篇关于从JavaScript中的Object.entries中的数组破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆