1

I've an array and an object. I should compare the Array value with the object key. If both are matches, I should push the value of object into the array.

I've achieve this with the below logic.

  var arr = [{ "name": "Coal", "segmentId": null }, { "name": "Ash", "segmentId": null }];

    var obj = {
        "Ash": {
            "October 2015": "66",
            "segmentId": "66",
            "December 2015": "435",
            "November 2015": "34535"
        },
        "Coal": {
            "October 2015": "23455",
            "segmentId": "66",
            "November 2015": "3454",
            "December 2015": "345"
        }
    };

    document.writeln("Original Array : " + JSON.stringify(arr));

    for (var i = 0; i < arr.length; i++) {
        var keys = Object.keys(obj);
        for (var j = 0; j < keys.length; j++) {
            if (arr[i].name === keys[j]) {
                arr[i].segmentId = obj[keys[j]].segmentId;
            }
        }
    }

    document.writeln("Transformed Array : " + JSON.stringify(arr));

I would like to know, Is there any other best way to handle this or any js library to make it simple?

Fiddler Version

4
  • perhaps look at underscorejs.org Commented Sep 29, 2015 at 13:11
  • Can you create jsfiddle? Commented Sep 29, 2015 at 13:34
  • Check our the fiddler here jsfiddler Commented Sep 29, 2015 at 14:18
  • @ergonaut can you name the underscorejs api name to handle this. Commented Sep 29, 2015 at 14:21

2 Answers 2

2

try this with javascript higher order functions:

arr.map(function(item){
  item["segmentId"] = obj[item["name"]]["segmentId"]
});
Sign up to request clarification or add additional context in comments.

1 Comment

Am getting two objects as undefined
0

Although you have not provided a fiddle.But you can do it in jquery like below:

Please note I am creating a new array for the result set.

var arr = [{ "name": "Coal", "segmentId": null }, { "name": "Ash", "segmentId": null }];
var obj = {
        "Ash": {
            "October 2015": "66",
            "segmentId": "66",
            "December 2015": "435",
            "November 2015": "34535"
        },
        "Coal": {
            "October 2015": "23455",
            "segmentId": "69",
            "November 2015": "3454",
            "December 2015": "345"
        }
    };

var a=[];  
$.each(arr, function(i, v) { 
    $.each(obj, function(oi, ov) {    
        if(arr[i].name==oi)             
             a.push(obj[oi].segmentId);           
    });
});


alert(a.length);

2 Comments

Why would I use jQuery for a plain old loop?
Hi @torazaburo OP has mentioned best way or any js library.So I think he is open to jquery.He is looking for best way.I have shown him one of the way.Of course it can be further reduced down to one liner.But I dont have much time now to spend time on this.Will look later

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.