0

I have the array with the different timestamp and based on that each timestamp there is some data related to this timestamp. for e.g. I have the data of whole day in array in that array there are the different timestamp and based on each timestamp there are some records based on this timestamp. So I want to search the records in array only from 4am to 6am.

    var object = {"main": [
    {"timestamp": "1am","name": "name 1"},
    {"timestamp": "2am","name": "name 2"},
    {"timestamp": "3am","name": "name 1"}, 
    {"timestamp": "4am","name": "name 2"},
    {"timestamp": "5am","name": "name 1"}, 
    {"timestamp": "6am","name": "name 2"},
    {"timestamp": "7am","name": "name 1"}, 
    {"timestamp": "8am","name": "name 2"},
    {"timestamp": "9am","name": "name 1"}, 
    {"timestamp": "10am","name": "name 2"}
    ]};
user3484089
  • 95
  • 10
  • You should provide relevant code examples so we can better help you. However, it sounds to me like [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) is what you need. – Ben Jan 03 '19 at 07:22
  • 1
    Map your timestamp to a number `Number(timeStamp.replace(/[ap]m/,'')) + (timeStamp.includes('pm')?12:0)` filter on this number with larger and smaller compare functions like [this example](https://stackoverflow.com/a/53850431/1641941) – HMR Jan 03 '19 at 07:58
  • Possible duplicate of [Javascript: How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – Tasos K. Jan 03 '19 at 12:45

3 Answers3

2

As explained by @ben Beck in the comment, you are looking for Array.prototype.filter() to return the filtered array and Array.prototype.includes() to find the needed timestamps

var object = {"main": [
    {"timestamp": "1am","name": "name 1"},
    {"timestamp": "2am","name": "name 2"},
    {"timestamp": "3am","name": "name 1"}, 
    {"timestamp": "4am","name": "name 2"},
    {"timestamp": "5am","name": "name 1"}, 
    {"timestamp": "6am","name": "name 2"},
    {"timestamp": "7am","name": "name 1"}, 
    {"timestamp": "8am","name": "name 2"},
    {"timestamp": "9am","name": "name 1"}, 
    {"timestamp": "10am","name": "name 2"}
    ]};
const from = 4; //4am
const to = 6; //6am

//generates the list of timestamps needed
const neededTimestamps = [];
for(let i = 4; i <= 6; i++){
  neededTimestamps.push(i + 'am');
}
console.log("Needed timestamps", JSON.stringify(neededTimestamps));  
 
const filtered = object.main.filter(o => neededTimestamps.includes(o.timestamp));

console.log(filtered);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
  • I get an error :-<--- Last few GCs ---> <--- JS stacktrace ---> ==== JS stack trace ========================================= FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory 1: node_module_register 2: v8::internal::FatalProcessOutOfMemory 3: v8::internal::FatalProcessOutOfMemory 4: v8::internal::Heap::CreateFillerObjectAt 5: v8::internal::Factory::NewFixedDoubleArray I also used node --max-old-space-size=4096 yourFile.js – user3484089 Jan 03 '19 at 08:56
  • Perhaps this is linked to another part of your code – Weedoze Jan 03 '19 at 08:59
  • I get another error :- TypeError: Cannot read property 'filter' of undefined – user3484089 Jan 03 '19 at 09:02
  • It means that `object.main` is undefined. Be sure that you are calling the correct array. This is a simple and clear error. You should try to fix it yourself instead of directly asking for help. It will help you to manage error in the future and debug them yourself – Weedoze Jan 03 '19 at 09:04
1

I assume you mentioned the values of timestamp as "4am", "5am" for readability. In general, those values will be integer values of EPOCH Unix timestamp in milliseconds.

So the approach is simple to use Array.prototype.filter()

var object = {"main": [
    {"timestamp": 1,"name": "name 1"},
    {"timestamp": 2,"name": "name 2"},
    {"timestamp": 3,"name": "name 1"}, 
    {"timestamp": 4,"name": "name 2"},
    {"timestamp": 5,"name": "name 1"}, 
    {"timestamp": 6,"name": "name 2"},
    {"timestamp": 7,"name": "name 1"}, 
    {"timestamp": 8,"name": "name 2"},
    {"timestamp": 9,"name": "name 1"}, 
    {"timestamp": 10,"name": "name 2"}
    ]};
    
    
    const filteredData = object.main.filter(o => o.timestamp >= 4 && o.timestamp <= 6);
    
    console.log(filteredData);
Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51
0

Use filter method to filter data from array

var object = {"main": [
    {"timestamp": "1am","name": "name 1"},
    {"timestamp": "2am","name": "name 2"},
    {"timestamp": "3am","name": "name 1"}, 
    {"timestamp": "4am","name": "name 2"},
    {"timestamp": "5am","name": "name 1"}, 
    {"timestamp": "6am","name": "name 2"},
    {"timestamp": "7am","name": "name 1"}, 
    {"timestamp": "8am","name": "name 2"},
    {"timestamp": "9am","name": "name 1"}, 
    {"timestamp": "10am","name": "name 2"}
    ]};

const filtered = object.main.filter(o => o.timestamp === '4am' || 
o.timestamp === '5am' || o.timestamp === '6am');

console.log(filtered)
  • You should add more information to explain your answer - For example: the function that you are using – Weedoze Jan 03 '19 at 09:00