user6378005
user6378005

Reputation:

Best Way to Filter Keys

I have data that looks like this:

var data = [
    {item1: 123, item2: "name", item3: "id"},
    {item1: 234, item2: "other"},
    {item1: 456, item2: "another"},
    {item1: 789, item2: "again"}
]

How do I loop through and find if item3 is there, and if it isn't, fill it with something.

Upvotes: 0

Views: 88

Answers (9)

le_m
le_m

Reputation: 20238

You could use a normal for loop iterating over the array elements or follow a functional approach.

var data = [
    {item1: 123, item2: "name", item3: "id"},
    {item1: 234, item2: "other"},
    {item1: 456, item2: "another"},
    {item1: 789, item2: "again"}
];

// 1. Using Array.foreach:
data.forEach(function(element) {
  if (!element.hasOwnProperty('item3')) element.item3 = "default";
});

// 2. Using Array.map: 
data = data.map(function(element) {
  if (!element.hasOwnProperty('item3')) element.item3 = "default";
  return element;
});

// 3. Using a for-loop:
for (var i = 0, length = data.length; i < length; ++i) {
  if (!data[i].hasOwnProperty('item3')) data[i].item3 = "default";
}

console.log(data);

I recommend 1 or 2 if you don't want to keep the original data array.

Performance on Chrome 48 (similar in Firefox 44, 100000 runs):

  • Array.foreach() 520ms
  • Array.map() 790ms
  • for-loop 2700ms

Possible explanation: Since the Array.foreach() and Array.map() callbacks do not reference anything outside the passed parameters, modern JITs can cache the passed inline function and outperform the traditional for-loop.

Upvotes: 2

Redu
Redu

Reputation: 26191

This is how you do it;

var data = [
    {item1: 123, item2: "name", item3: "id"},
    {item1: 234, item2: "other"},
    {item1: 456, item2: "another"},
    {item1: 789, item2: "again"}
],
 newData = data.map(o => !o.item3 ? (o.item3 = "something",o) : o);

console.log(newData);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386736

While the specification is filtering for objects without item3, i suggest to use Array#filter.

var data = [{ item1: 123, item2: "name", item3: "id" }, { item1: 234, item2: "other" }, { item1: 456, item2: "another" }, { item1: 789, item2: "again" }],
    dataWithoutItem3 = data.filter(function (o) {
        return !('item3' in o);
    });

document.write('<pre>' + JSON.stringify(dataWithoutItem3, 0, 4) + '</pre>');

Upvotes: 0

Alexey Ayzin
Alexey Ayzin

Reputation: 209

for(var elem in data){
    var isPresent = 0;
    for(var key in data[elem]){
        if(key == "item3"){
            isPresent = 1;
        }
    }
    if(!isPresent){
        //Whatever function you need
    }

This should be it.

Upvotes: 0

Michał Perłakowski
Michał Perłakowski

Reputation: 92579

Use a simple for...of loop:

for (const element of data) {
  if (!("item3" in element)) {
    element.item3 = "something"
  }
}

Upvotes: 1

pparas
pparas

Reputation: 549

Try the following loop:

       for (var i = 0; i < data.length; i++){
            for (var item in data[i]){
            if (item == "item3"){
                console.log("found");
            }
          }
        }

Upvotes: 1

IMTheNachoMan
IMTheNachoMan

Reputation: 5843

Iterate through and check if a key exists.

var data = [
    { item1: 123, item2: "name", item3: "id" },
    { item1: 234, item2: "other" },
    { item1: 456, item2: "another" },
    { item1: 789, item2: "again" }
]

function fillIn(data, key, value)
{
    for(var i = 0, numRows = data.length; i < numRows; ++i)
    {
        if(!(key in data[i])) data[i][key] = value;
    }
}

fillIn(data, "item3", "dingo");

Upvotes: 1

mdickin
mdickin

Reputation: 2385

data.forEach(function (item) { 
    if (item.item3 == undefined) item.item3 = "filled with something"
});

Upvotes: 1

element11
element11

Reputation: 4485

You can use a for loop to iterate over each item, in order, in the array.

for(var i = 0; i < data.length; i++) {
    //check to see if item3 is NOT there
    if(typeof data[i].item3 === 'undefined') {
        data[i].item3 = 'default value';
    }
}

Upvotes: 1

Related Questions