0

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Getting an error - TS2339: Property 'map' does not exist on type 'SAInterface'.
Click on Run code snippet and see the console log output. If you're getting that error it means your saResponds "array object" is not an array at all. You'll have to give us the real shape before we can help you. Also this is a typescript error. You asked a Javascript question. You have to do your own types here
The answer with .values(saResponse[0]) is working fine. saResponse is an object and i have updated my question accordingly. Now, in case of multiple values i want them to be separated with , Please let me know the updated answer. Thanks!!
1
// 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('::'))

Comments

1

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

10 Comments

I am getting [object Object] when trying to do console.log(result)
For the answer that I posted, I'm getting the result "Low::Type1::Notes1::2020-07-29" which looks correct to me. Please note, in my code-snippet, "saResponse" is an object and not an array. And I addressed my confusion in the answer regarding this.
When i check details for saResponse, I see below Array(1) 0: {severity: "Low", assignee: null, notesType: "Type1", notes: "Notes1", createdAt: "2020-07-29"} length: 1 __proto__: Array(0)
I am getting an error - Getting an error - TS2339: Property 'map' does not exist on type 'SAInterface'.
This is working fine - const saValue = Object.values(saResponse[0]).filter(val => val) .join('::'); But, how to address multiple elements using above?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.