简体   繁体   English

如何使用扩展运算符但不从 object 获取值 null

[英]How to use spread operator but not get values null from the object

I have an array of object, something like this:我有一个 object 的数组,如下所示:

array = [
  {
    'propertyName1': 'name1',
    'propertyValue1': 'value1',
    'propertyName2': 'name2',
    'propertyValue2': 'value2',
    'propertyName3': 'name3',
    'propertyValue3': 'value3',
    'propertyName4': 'name4',
    'propertyValue4': 'value4',
  },
  {
    'propertyName1': 'name10',
    'propertyValue1': 'value10',
    'propertyName2': 'name22',
    'propertyValue2': 'value22',
    'propertyName3': 'name33',
    'propertyValue3': 'value33',
    'propertyName4': null,
    'propertyValue4': null,
  }
]

I want to get the first value as a key and the second value as a value in a new object, something like this result:我想在新的 object 中将第一个值作为键,将第二个值作为值,结果如下:

{
  name1: "value1"
  name10: "value10"
  name2: "value2"
  name22: "value22"
  name3: "value3"
  name33: "value33"
  name4: "value4"
  null: null
}

but I don't want to show the property with the value null, so I tried:但我不想显示值为 null 的属性,所以我尝试了:

ngOnInit() {
  let obj = {};
  this.array.forEach((element: any) => {
    obj = {
      ...obj,
      [element.propertyName1]: element.propertyValue1,
      [element.propertyName2]: element.propertyValue2,
      [element.propertyName3]: element.propertyValue3,
      [element.propertyName4]: element.propertyValue4}
    }
  );
    
  console.log(obj);
}

See the code here: Stackblitz请参阅此处的代码: Stackblitz

Just delete the null entry after只需删除 null 条目

this.array.forEach((element: any) => {
      obj = {
        ...obj,
        [element.propertyName1]: element.propertyValue1,
        [element.propertyName2]: element.propertyValue2,
        [element.propertyName3]: element.propertyValue3,
        [element.propertyName4]: element.propertyValue4,
      };
    });

    delete obj['null'];

https://stackblitz.com/edit/angular-ivy-gaqes8?file=src/app/app.component.ts https://stackblitz.com/edit/angular-ivy-gaqes8?file=src/app/app.component.ts

You can spread all the values in a single array and check if the property is null while looping over:您可以将所有值分布在一个数组中,并在循环时检查属性是否为 null:

  ngOnInit() {
    const valuesArray = [ //Array containing all the values
      ...Object.values(this.array[0]),
      ...Object.values(this.array[1]),
    ];
    let obj = {};
    for (let i = 0; i < valuesArray.length; i = i + 2) {
      if (valuesArray[i + 1] !== null) {
        obj[valuesArray[i]] = valuesArray[i + 1]; //Only copy when it is not null
      }
    }

    console.log(obj);
  }

If your first array also might contain null values and you want to exclude them you can use this if condition instead of the one above:如果您的第一个数组也可能包含 null 个值并且您想排除它们,您可以使用此 if 条件而不是上面的条件:

if (valuesArray[i + 1] !== null && valuesArray[i] !== null ) 

 const data = [{"propertyName1":"name1","propertyValue1":"value1","propertyName2":"name2","propertyValue2":"value2","propertyName3":"name3","propertyValue3":"value3","propertyName4":"name4","propertyValue4":"value4"},{"propertyName1":"name10","propertyValue1":"value10","propertyName2":"name22","propertyValue2":"value22","propertyName3":"name33","propertyValue3":"value33","propertyName4":null,"propertyValue4":null}] let r = {} // get maximum number of keys that may need to be iterated over let n = data.map(o=>Object.keys(o).length).reduce((a,c)=>Math.max(a,c)) for(let i=1; i<=n; i++) { data.forEach(o=> { let x = o[`propertyName${i}`] let y = o[`propertyValue${i}`] if (x && y) r[x] = y }) } console.log(r)

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

相关问题 展开运算符如何使用 Javascript 中的迭代器/生成器从对象中获取值? - How does spread operator get values from an object with an Iterator/Generator in Javascript? 如何使用扩展运算符使用扩展运算符使用反应从对象数组中删除 object? - How to use spread operator to delete an object from array of objects using spread operator using react? 需要知道如何对对象使用扩展运算符 - Need to know how to use the spread operator for an object 如何使用对象解构扩展运算符来删除多个对象属性 - How to use object destructuring spread operator to delete multiple object properties 扩展运算符的缺失值 - missing values from spread operator 如何使用扩展运算符将属性添加到对象,而不是覆盖它? - How to use spread operator to add property to an object, not override it? 如何使用spread运算符从对象数组中删除重复项 - How to remove duplicates from an object array using spread operator 有没有办法使用扩展运算符合并两个对象,使得结果具有一个 object 的键,但第二个的值? - Is there way to use the spread operator to merge two objects such that the result has the keys of one object, but the values of the second? 如何使用传播算子更新对象 - How to update object using spread operator JS - 如何用扩展运算符和条件替换 object? - JS - how to replace an object with spread operator AND condtion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM