0

I need to obtain from a given array of objects, all different values for a given key. Example:

 var arr =   [
      {
        "cod": 0
        "level": "INF"
      },{
        "cod": 1
        "level": "INF"
      },{
        "cod": 2
        "level": "INC"
      },{
        "cod": 3
        "level": "IND"
      }
]

different_values(arr,'level')

should return:

['INF','INC','IND']

WHat would be an easy way to achieve so?

5 Answers 5

5

Loop over the array and get the specified property from each element. use them as the keys in an object to get rid of the duplicates, and then return the keys of that object.

var arr =   [
      {
        "cod": 0,
        "level": "INF"
      },{
        "cod": 1,
        "level": "INF"
      },{
        "cod": 2,
        "level": "INC"
      },{
        "cod": 3,
        "level": "IND"
      }
];
function different_values(array, property) {
    var values_seen = {}; // for removing duplicates
    for (var i = 0; i < array.length; i++) {
        values_seen[array[i][property]] = true;
    }
    return Object.keys(values_seen);
}
alert(JSON.stringify(different_values(arr, 'level')));

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

1 Comment

Yeah, I like this method of deduping.
2
function different_values(arr, value){
    var res = [];
    for(var i=arr.length;i--;){
        if(res.indexOf(arr[i][value]) === -1){
            res.push(arr[i][value])
        }
    }
    return res;
}

DEMO https://jsfiddle.net/mdzLr4q6/1/

Comments

0

try this:

function different_values(arr,key){
    var diff=[];
    for(var i = 0;i < arr.length;i++){
        var k=arr[i][key];
        if(diff.indexOf(k)===-1)diff.push(k);
    }
    return diff;
}

3 Comments

You should avoid using for in loops for arrays. for (var i = 0, l = arr.length; i < l; i++) would be better. Plus you get to cache the length of the array.
why should I avoid it ?
-1
function different_values(array, key) {

    var count = array.length,
        output = [];

    for (var i = 0; i < count; i++) {
        output.push(array[i][key]);
    }

    return output;
}

2 Comments

You're not removing duplicates from the output array which is what the OP wants.
Oops didn't notice the word "different"
-1

Just loop through the objects in a for loop and push the level in a result array.

Edit: added indexOf to get unique values

function different_values (arr, key) {
    var result = [];

    for (var i in arr) {
        if (result.indexOf(arr[i][key]))            
            result.push(arr[i][key]);
    }

    return result;
}

Make sure to correct your objects. You are missing a comma between the elements:

{
    "cod": 0
    "level": "INF"
}

should be:

{
    "cod": 0,
    "level": "INF"
}

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.