2

I tried several of the map functions but could not find a proper way to get what I want. Here is the case:

Object {Results:Array[3]}
   Results:Array[3]
       [0-2]
            0:Object
                   id=null     
                   name: "Rick"
                   upper:"0.67"
            1:Object
                   id="00379321"     
                   name:null
                   upper:"0.46"
            2:Object
                   id="00323113"      
                   name:null
                   upper:null

I want my final result to look like this. I wanted all null values to be removed and all the entries tied up like this in an object.

var finalResult = ["Rick","0.67","00379321","0.46","00323113"];

How can I achieve this result?

4 Answers 4

3
_.chain(a)
.map(function(x) {return _.values(x)})
.flatten()
.filter(function(x) {return x != null;})
.value()
Sign up to request clarification or add additional context in comments.

7 Comments

Note that the "null" values in the example code are not actually null, but rather a string. Also, it's generally better to provide an explanation of the code, especially to people who are not familiar with the library.
@MikeMcCaughan to people who are unfamiliar with library I always suggest to read docs first.
Okay, but take a look at How to Answer, specifically Brevity is acceptable, but fuller explanations are better. It's obviously up to you how helpful you want to be, but this will likely end up on the Low Quality review queue due to being a code dump.
@Andrey- this still shows null values at the final output when i do console.log(a); also i dont see any errors. am i doing anything wrong ?
@Patrick I don't know what nulls you have exactly. Normal null value in JS is a value on its own, but in your example you have "null" as string. Adjust my code accordingly (4th line)
|
3

Small modification for @andrey's code (requires lodash.js)

var a = [{id:null, name: "Rick", upper:"0.67"}, {id:"00379321", name:null, upper:"0.46"}, {id: "00323113",name:null, upper:null}]
_(a)
.map(function(x) {return _.values(x)})
.flatten()
.without(null)
.value()

2 Comments

my apologies. The input in the console shows a bit different than wat i had earlier mentioned. Can you please have a look and let me know
I updated my answer. I used different data in the array. Now it contains the same data and the result is the same
2

I suggest to use a fixed array for the keys, because the properties of an object have no order and the order is relevant.

var data = [{ id: null, name: "Rick", upper: "0.67" }, { id: "00379321", name: null, upper: "0.46" }, { id: "00323113", name: null, upper: null }],
    result = [];

data.forEach(function (a) {
    ['id', 'name', 'upper'].forEach(function (k) {
        if (a[k] !== null) {
            result.push(a[k]);
        }
    });
});

console.log(result);

7 Comments

it gives me an error stating data.forEach is not a function
which user agent do you use?
didnt get you by user agent
what browser do you use?
its working fine with chrome 51.0.2704.106 m for windows.
|
1

Another underscore solution, similar to the other underscore solutions, but uses reject and the isNull predicate:

var result = _.chain(data)
    .map(_.values)
    .flatten()
    .reject(_.isNull)
    .value();

Comments

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.