typescript 如何使用spread运算符但不从对象中获取null值

baubqpgj  于 2022-12-14  发布在  TypeScript
关注(0)|答案(4)|浏览(126)

我有一个对象数组,如下所示:

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,
  }
]

我想把第一个值作为键,第二个值作为新对象中的值,结果如下:

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

但我不想显示值为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);
}

请在此处查看代码:Stackblitz

pgvzfuti

pgvzfuti1#

仅删除空条目

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

cngwdvgl

cngwdvgl2#

您可以将所有值分散在单一数组中,并在循环时检查属性是否为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条件代替上面的条件:

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

2g32fytz3#

在TS操场上跑步

const indexes = [1, 2, 3, 4] as const;
type ObjIndex = typeof indexes[number];

type Obj = Record<`propertyName${ObjIndex}` | `propertyValue${ObjIndex}`, string | null>;

const array:Obj[] = [{
    '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,
      }];

const result: Record<string, string | null> = {};

for(const obj of array) {
    for(const idx of indexes){
        const key = obj[`propertyName${idx}`];
        const val = obj[`propertyValue${idx}`];
        key && val && (result[key] = val);
    }
}

console.log(result);
oprakyz7

oprakyz74#

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)

相关问题