2

In the object array received from the server, I want to process and retrieve only the non-null value of the property in the object. How can I change it in my function??

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', 
}]
ArunPratap
  • 4,816
  • 7
  • 25
  • 43
sunpl13
  • 147
  • 4
  • 13
  • 1
    Does this answer your question? [Remove empty elements from an array in Javascript](https://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript) – Brendan Bond Jan 02 '22 at 04:38

5 Answers5

3

A concise way to remove null values is to filter object entries, creating an object from those remaining. With that, just map over the input array...

function nonNullValues(obj) {
  return Object.fromEntries(
    Object.entries(obj).filter(([key, value]) => value !== null)
  )
}
const result = theArray().map(nonNullValues);
console.log(result)

function theArray() {
  return [{
      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,
    }
  ]
}
danh
  • 62,181
  • 10
  • 95
  • 136
3

Lodash if you don't mind. Using: omitBy and isNull

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 => _.omitBy(obj, _.isNull));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
Valentine Shi
  • 6,604
  • 4
  • 46
  • 46
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
0

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 nonNull = []
for(const item of arr) {
    const obj = {}
    for(const key in item) {
        if(item[key]) obj[key] = item[key]
    }
    nonNull.push(obj)
}
console.log(nonNull)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 02 '22 at 07:45
0

Using your current logic you were just a couple steps away first create this function (or you can change to a method):

const getDataArr = (arr) => {
    const newArr = [];
    for (const item of arr) {
        const currObject = {};
        for (const key in item) {
            if (item[key] !== null) currObject[key] = item[key];
        }
        newArr.push(currObject);
    }
    return newArr;
};

Then you can print its result using:

console.log(getDataArr(arr));
0

this way

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
    } 
  ] 
arr.forEach(obj=>
  {
  Object.keys(obj).forEach(key =>
    {
    if (obj[key] === null) delete obj[key] 
    })
  })

console.log( arr)
.as-console-wrapper {max-height: 100%!important;top:0 }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40