3

I want to receive a single object array and return only the non-null values ​​in the object arrays.

However, the method I have implemented contains non-null properties in an array and returns them as an array. How can I make it easier?

I want it to be returned as an object, not as an array.

const arr = [{
text01 : 'name',
text02 : 'email@gmail.com',
text03 : '010-1234-5678',
text04 : 'adress1',
text05 : 'adress2',
text06 : null,
text07 : null,
text08 : null,
},
{
text01 : 'name1',
text02 : 'email2@gmail.com',
text03 : '010-1255-5148',
text04 : 'adress3',
text05 : 'adress4',
text06 : null,
text07 : null,
text08 : null,
}]    
getDataArr(arr) {
            arr.forEach(item => {
                const aaa = [];
                for (let key in item) {
                    if (item[key] !== null) {
                        const value = item[key];
                        aaa.push({ key, value });
                    }
                }
                console.log(aaa);
            });

        // Get the value as const arr = [{
text01 : 'name',
text02 : 'email@gmail.com',
text03 : '010-1234-5678',
text04 : 'adress1',
text05 : 'adress2',
},
{
text01 : 'name1',
text02 : 'email2@gmail.com',
text03 : '010-1255-5148',
text04 : 'adress3',
text05 : 'adress4',
}]    
        },
sunpl13
  • 147
  • 4
  • 13
  • 1
    This is even easier these days. You don't have to write this function... you can use `Object.entries()`, `Array.filter()`, and `Object.fromEntries()` to do it all for you. See also: https://stackoverflow.com/a/62400741/362536 – Brad Jan 01 '22 at 07:32
  • 2
    Add your array and expected result... – DecPK Jan 01 '22 at 07:37
  • You already *are* getting an array of objects, not of arrays? – Bergi Jan 01 '22 at 09:05
  • @decpk,I have added an example, please check. – sunpl13 Jan 02 '22 at 03:17
  • @sunpl13 what is your expected result? Please add the expected `code` in the question itself? – DecPK Jan 02 '22 at 06:38

2 Answers2

0

You could filter empty objects, undefined and null values from an array of objects like this:

const data = [
  {name: "joe", age:99},
  {name: "jack", age:8},
  undefined,
  {name: "lola", age:54},
  {},
  null
]

function getValidArray(arr){
  return arr.filter(obj=> {
    if(!!obj && typeof obj === "object"){
      return Object.keys(obj).length > 0
    }
    return !!obj
  })
}

console.log(getValidArray(data))

EDIT:

you can achieve the desired result of your updated thread like this:

const arr = [{
text01 : 'name',
text02 : 'email@gmail.com',
text03 : '010-1234-5678',
text04 : 'adress1',
text05 : 'adress2',
text06 : null,
text07 : null,
text08 : null,
},
{
text01 : 'name1',
text02 : 'email2@gmail.com',
text03 : '010-1255-5148',
text04 : 'adress3',
text05 : 'adress4',
text06 : null,
text07 : null,
text08 : null,
}]    

function removeNullEntries(arr){
  return arr.map(obj=> {
    const entries = Object.entries(obj)
    const validEntries = entries.filter(([key, value])=> !!value)
    return Object.fromEntries(validEntries)
  })
}

console.log(removeNullEntries(arr))
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
  • I didn't get the results I wanted. I added an example above, so please check it out. – sunpl13 Jan 02 '22 at 03:18
  • It's done. I have edited my answer with a custom function that solves your usecase. Feel free to ask for explanations if you have any question about the code. If the answer suits you, please mark it as valid so other people can benefit from a solved thread. – DoneDeal0 Jan 02 '22 at 08:33
0

You can easily achieve the result using filter and map as a single-liner:

arr.map((obj) =>
  Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== null))
);

const arr = [
  {
    text01: 'name',
    text02: 'email@gmail.com',
    text03: '010-1234-5678',
    text04: 'adress1',
    text05: 'adress2',
    text06: null,
    text07: null,
    text08: null,
  },
  {
    text01: 'name1',
    text02: 'email2@gmail.com',
    text03: '010-1255-5148',
    text04: 'adress3',
    text05: 'adress4',
    text06: null,
    text07: null,
    text08: null,
  },
];

const result = arr.map((obj) => Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== null)));

console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
DecPK
  • 24,537
  • 6
  • 26
  • 42