Home > Blockchain >  How to use spread operator but not get values null from the object
How to use spread operator but not get values null from the object

Time:12-08

I have an array of object, something like this:

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:

    {
    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:

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

CodePudding user response:

Just delete the null entry after

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

CodePudding user response:

You can spread all the values in a single array and check if the property is null while looping over:

  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:

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

CodePudding user response:

Run on TS playground

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);


CodePudding user response:

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)

  • Related