Saikiran
Saikiran

Reputation: 776

How to solve this without using three nested for loops

I have an array of objects something like this

var data = [{"2017-09-13":{date_time:"2017-09-13",value:"20"}},{"2017-09-13":{date_time:"2017-09-13",value:"22"}},{"2017-09-15":{date_time:"2017-09-15",value:"25"}},{"2017-09-15":{date_time:"2017-09-15",value:"30"}},{"2017-09-16":{date_time:"2017-09-16",value:"10"}}];

I have an array of dates like this

var dates = ["2017-09-13","2017-09-15"];

I want to modify the data array in such a way that it only contains the days mentioned in the dates array. I have tried something like this

var date = [];
for (var i = 0; i < data.length; i++) {
        for (var j = 0; j < dates.length; j++) {
            for (key in data[i]) {
                if (dates[j] == key) {
                    date.push(data[i])
                }
            }
    }
}

And it gives me the required result. However this is not efficient and is lagging the application. Is there any efficient way to go about it?

EDIT: Updated the correct data structure

Upvotes: 0

Views: 65

Answers (4)

Jonas Wilms
Jonas Wilms

Reputation: 138277

Your datastructure is ugly. You will always need two loops to iterate it. However, we could set up a more elegant datastructure ( aka a Map), which we can access more easily:

 const days = new Map();

 for(const obj of data){
   for(day in obj){
    if( days.has(day) ){
      days.get(day).push( obj[day] );
    } else {
      days.set(day, [ obj[day] ]);
    }
  }
}

After the Map is created, you can simply do:

days.get("2017-09-13")

to get an array of objects with datetime/values. That can be iterated easily:

days.get("2017-09-13").forEach( ({value}) => {
  console.log(value);
});

Or getting multiple dates:

 const result = new Map(
   dates.map(date => [date, days.get( date )] )
 );

 console.log( [...result] );

and data only:

const result = [];
dates.forEach(date => result.push(...days.get(date)));

Upvotes: 1

ideaboxer
ideaboxer

Reputation: 4101

const data = [
    {
        "2017-09-13": {
            "date_time": "2017-09-13",
            "value": "20"
        }
    },
    {
        "2017-09-13": {
            "date_time": "2017-09-13",
            "value": "22"
        }
    },
    {
        "2017-09-15": {
            "date_time": "2017-09-15",
            "value": "25"
        }
    },
    {
        "2017-09-15": {
            "date_time": "2017-09-15",
            "value": "30"
        }
    },
    {
        "2017-09-16": {
            "date_time": "2017-09-16",
            "value": "10"
        }
    }
];

const dates = ["2017-09-13", "2017-09-15"];
const datesSet = new Set(dates);

const filteredData = data.filter(item => datesSet.has(Object.keys(item)[0]));
console.log(filteredData);

Consider removing the use of dates as keys, as they seem to be redundant information:

const data = [
    {
        "date_time": "2017-09-13",
        "value": "20"
    },
    {
        "date_time": "2017-09-13",
        "value": "22"
    },
    {
        "date_time": "2017-09-15",
        "value": "25"
    },
    {
        "date_time": "2017-09-15",
        "value": "30"
    },
    {
        "date_time": "2017-09-16",
        "value": "10"
    }
];

const dates = ["2017-09-13", "2017-09-15"];
const datesSet = new Set(dates);

const filteredData = data.filter(item => datesSet.has(item.date_time));
console.log(filteredData);

Upvotes: 2

Luka967
Luka967

Reputation: 141

var dataByDate = { };
for (var i = 0; i < data.length; i++) {
    for (var j in data[i])
        dataByDate[j] = dataByDate[j] || true;
}

Converts the data to

{ '2017-09-13': true, '2017-09-15': true, '2017-09-16': true }

Then you can do

for (var i in dataByDate) { ... }

Upvotes: 0

JSON Derulo
JSON Derulo

Reputation: 17580

Here is a solution with only two for loops.

var data = [{'2017-09-13':{date_time:"2017-09-13",value:"20"}},{'2017-09-13':{date_time:"2017-09-13",value:"22"}},{'2017-09-15':{date_time:"2017-09-15",value:"25"}},{'2017-09-15':{date_time:"2017-09-15",value:"30"}},{'2017-09-16':{date_time:"2017-09-16",value:"10"}}];

var date = [];
for (var i = 0; i < data.length; i++) {
      for (key in data[i]) {
          if (date.indexOf(key) === -1)  {
             date.push(key);
          }
      }
}

console.log(date);

Upvotes: 0

Related Questions