0

Is there quick way to filter an array of objects to return only a few properties in each object?

For example we have the data below:

var objArr = [{

    "Title": "July 13 - July 19 2014",
        "displayAd_imp": "3,500",
        "videoAd_imp": "1.5",
        "tv_imp": "0.52",
        "Date": "2014-07-17T00:00:00.000Z",
        "WeekNo": 29
}, {

    "Title": "July 20 - July 26 2014",
        "displayAd_imp": "1,600",
        "videoAd_imp": "2.55",
        "tv_imp": "0.052",
        "Date": "2014-07-24T00:00:00.000Z",
        "WeekNo": 30
}, {

    "Title": "July 27 - Aug 2 2014",
        "displayAd_imp": "1,500",
        "videoAd_imp": "2.1",
        "tv_imp": "0.122",
        "Date": "2014-07-31T00:00:00.000Z",
        "WeekNo": 31
}]

I'm trying to filter the array above to get another array with only videoAd_imp, videoAd_imp, tv_imp. so it would look like this:

 [{

            "displayAd_imp": "3,500",
            "videoAd_imp": "1.5",
            "tv_imp": "0.52",
    }, {

            "displayAd_imp": "1,600",
            "videoAd_imp": "2.55",
            "tv_imp": "0.052",

    }, {

            "displayAd_imp": "1,500",
            "videoAd_imp": "2.1",
            "tv_imp": "0.122",
}]

Thanks in advance!

3
  • If you want to modify the original objects, just loop over them, and use delete to remove the stuff you don't want. If you want new objects, use .map() and return new objects with only the stuff you do want. There's no built-in solution. Commented Aug 28, 2014 at 17:43
  • Is there a reason you don't want to use the object as is? (Not saying you should; just curious.) Commented Aug 28, 2014 at 17:45
  • ...is this data arriving as JSON data? If so, this can be done automatically using JSON.parse(). Commented Aug 28, 2014 at 17:45

3 Answers 3

9

Use Array.map like bellow

If you want new references to all objects.

var newArr = objArr.map(function (obj) {
     return {displayAd_imp:obj.displayAd_imp,videoAd_imp:obj.videoAd_imp,tv_imp:obj.tv_imp};
})

console.log(newArr);

If you want to get original references use like bellow

var newArr = objArr.map(function (obj) {
    delete obj.Date;
    delete obj.WeekNo;
    delete obj.Title
    return obj;
})
Sign up to request clarification or add additional context in comments.

Comments

3
var objArr = [{/* ... */}];

// cache the array length to avoid resolving it on every iteration
var arrLength = objArr.length;
// your new array
var newArr = [];

for(var i=0; i<arrLength; i++){
    // push only the properties you want to the new array
    newArr.push({
        displayAd_imp: objArr[i].displayAd_imp,
        videoAd_imp: objArr[i].videoAd_imp,
        tv_imp: objArr[i].tv_imp
    });
}
// show it in the console
console.log(newArr);

JS Fiddle Demo

Comments

2

Here is a simple solution

var newArr = []

for(var i=0;i<objArr.length) {
    newArr.push({
        displayAd_imp: objArr[i].displayAd_imp,
        videoAd_imp: objArr[i].videoAd_imp,
        tv_imp: objArr[i].tv_imp
       }
    );
}

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.