简体   繁体   English

从对象数组中获取所有真实值

[英]Get all truthy values from an array of objects

I want to get all truthy values from an array of objects:我想从对象数组中获取所有真实值:

 const users = [{ name: 'string', val: false, }, { name: true, val: [], } ] const identifiers = users.map(i => Object.entries(i)) const active = identifiers.filter(function(id) { const t = id.map((i, k) => id[k][1]) return t }) console.log(active)

The idea of the code above at the end to use Object.fromEntries() and to get something like this:最后上面代码的想法是使用Object.fromEntries()并得到这样的东西:

 const users = [{ name: 'string, }, { name: true, } ]

At the moment i am blocked and i can't get the expected values.目前我被阻止了,我无法获得预期值。 Who can help to get the expected result?谁能帮助获得预期的结果?

You can use Object.fromEntries to create an object using just the properties that are truthy :您可以使用Object.fromEntries来创建 object ,仅使用以下属性

 const users = [{ name: true, val: false, }, { name: true, val: true, } ] const identifiers = users.map(e => { // Map User array return Object.fromEntries( // return new object for every element Object.entries(e).filter((o) => o[1] === true) // object constructed using just truthy values by filtering object o[1] -> value ) }); console.log(identifiers)

Or a beautiful one liner:或者一个漂亮的衬里:

const identifiers = users.map(e => Object.fromEntries(Object.entries(e).filter(([_,o]) => o)));

You can use reduce method with Object.entries and Object.fromEntries您可以对Object.entriesObject.fromEntries使用reduce方法

let result = users.reduce((acc,i) => { 
                 let obj = Object.entries(i).filter(([k,v]) => v); 
                 acc.push(Object.fromEntries(obj)); 
                 return acc;  
               }, [])

console.log(result);

You can decompose, filter and reconstruct the objects like this:您可以像这样分解、过滤和重建对象:

 const users = [{name:true,val:false,},{name:true,val:[],dd:0,ee:1,ff:[0,1]}], res = users.map(i => Object.fromEntries(Object.entries(i).filter(([_,v])=>.Array.isArray(v)&&v || v.length)) ) console.log(res)

This should do the trick:这应该可以解决问题:

const newData = users.map(user => {
  return Object.keys(user).reduce((obj, key) => {
    if (user[key]) { //for strict truthy, make user[key] === true
      obj[key] = user[key];
    }
    return obj;
  } ,{});
});

That will:这将:

  1. Perform the object filter operation on each element of the array (map).对数组(映射)的每个元素执行 object 过滤操作。
  2. Iterate over each key of the object (Object.keys(user).reduce)遍历 object (Object.keys(user).reduce) 的每个键
  3. Only add a key value pair if the value is truthy.如果值是真的,只添加一个键值对。

Note that this will return a new structure, not modify the existing one.请注意,这将返回一个新结构,而不是修改现有结构。 I saw the update to the question and put a comment in the code accordingly.我看到了问题的更新,并相应地在代码中添加了评论。

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

相关问题 如何 map 真实值将 object 中的 js/lodash 中的对象分隔到数组中? - How to map truthy values to separate objects in js/lodash in an object to array? 如何从ejs数组中的所有对象获取所有使用的值 - How to get all used values from all objects in array in ejs Javascript 如何从对象数组中获取属性的所有值? - Javascript How to get all values for a property from an Array of Objects? javascript 中真值的数组 arrays - Array of arrays of truthy values in javascript 从对象数组获取值 - Get values from an array of objects 从对象获取值到数组 - Get values from objects into array JavaScript:创建一个函数来拒绝被认为是真实的数组元素,麻烦数组对象中的值 - JavaScript: Creating a function to reject array elements found to be truthy, trouble with values in objects in the array 如何获取对象数组中对象的所有属性值的数组? - How to get an array of the values of all properties of objects inside an array of objects? 如何从具有嵌套 arrays 对象的对象数组中获取所有特定值? - How to get all specific values from an array of objects with nested arrays of objects? 遍历包含对象的 arrays 数组并从对象中获取所有特定值,然后显示到页面 JQuery - Loop through array of arrays which contain objects and get all specific values from the objects then show to the page JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM