meallhour
meallhour

Reputation: 15581

How to fetch only the values from an object ignoring the null values in NodeJS?

I have an object saResponse which looks as below:

0: {severity: "Low", assignee: null, notesType: "Type1", notes: "Notes1", createdAt: "2020-07-29"}
length: 1

I want to convert this Object into String such that only values are displayed separted by :: and ignoring null values.

Desired Output

Low::Type1::Details1::2020-07-29

I have added below code but it is not giving desired output

 const saValue = Object.values(saResponse).map
                (el => Object.entries(el).filter(([key, value]) => value !== null).reduce((acc, [key, value]) => ({
                    ...acc,
                    [key]: value
                }), {}));
const saStringValue = JSON.stringify(saValue);

Upvotes: 0

Views: 617

Answers (3)

Dominik
Dominik

Reputation: 6313

I think this should work and make it easier.

const saResponse = {
    0: {severity: "Low", assignee: null, notesType: "Type1", notes: "Notes1", createdAt: "2020-07-29"},
    1: {severity: "High", assignee: "meallhour", notesType: null, notes: "Notes1", createdAt: "2021-07-29"},
};

const saValue = Object.values(saResponse)
    .map((row) => Object
        .values(row)
        .filter(val => val)
        .join('::')
).join(',');

console.log(saValue);

Upvotes: 1

Kartik Chauhan
Kartik Chauhan

Reputation: 3068

From what I could deduce from your post and comments, I think the structure for the variable saResponse is this:

saResponse

You have an array with only a single item in it as the length property's value is 1. The item which is in it is an object.

Now, in your case, you only have a single item but I'm posting answers for both scenarios where you've only one item and in the second one multiple items in the array. Please make suitable changes to the code-snippet below according to your needs.

let saResponse = [
    { severity: "Low", assignee: null, notesType: "Type1", notes: "Notes1", createdAt: "2020-07-29" },
    { severity: "High", assignee: 'john', notesType: null, notes: "Notes1", createdAt: "2020-06-27" }
];

// For array with only one element
let result = Object.values(saResponse[0]).filter(el => el !== null).join('::');

// For array with multiple elements
let result2 = saResponse.map(obj => Object.values(obj).filter(el => el !== null).join('::'))

Also, I've tested the value only against null as you specifically mentioned you didn't want null values. However, I'm not sure if you have a good idea about falsy values because among many falsy values in javascript, null is one of them. Other falsy values are undefined, 0, ""(empty string), false etc. Please read this if you want more information on this.

If you don't want any falsy values then you could simply replace .filter(el => el !== null) with .filter(el => el).

Upvotes: 1

mr__pablo
mr__pablo

Reputation: 71

// i have tried this and it work, but i cannot get where you get `Details 1` string?
const object1 = {severity: "Low", assignee: null, notesType: "Type1", notes: "Notes1", 
createdAt: "2020-07-29"};

const arr = Object.values(object1);
const filtered = arr.filter( el=>( 
   el != null)
);

console.log(filtered.join('::'))

Upvotes: 1

Related Questions