Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Value in between javascript

I have an arrays of object,

var date = [{age:5, name:"al"},
              {age:10, name:"ben"},
              {age:15, name:"mac"},
              {age:15, name:"sarah"},
              {age:20, name:"jack"},
              {age:24, name:"dan"}
             ];

and

var age = [15, 23];

For each age, how can I filter the array (named "data") that contain the exact match, and if there is no exact match, it will return the element that in between (closest lower value & closest upper value)?

For example for this case, for each age (15 and 23) it supposed to give me:

[[{age:15, name:"mac"},{age:15, name:"sarah"}],[{age:20, name:"jack"},{age:24, name:"dan"}]].

I try to use this:

function int(){
  var data = [{age:5, name:"al"},
              {age:10, name:"ben"},
              {age:15, name:"mac"},
              {age:15, name:"sarah"},
              {age:20, name:"jack"},
              {age:24, name:"dan"}
             ];
  var age = [15, 23];
  var filterData = age.map(n => data.filter(v => n==v.age));
}

But it only resolved the exact match value. However, I cant figured out how to get for in between values (closest lower & closest upper). In this case, for age 23, i supposed to get [{age:20, name:"jack"},{age:24, name:"dan"}].

Any help or tips would be appreciated. I have searched and cannot find a solution.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const data = [
  {
    age: 5,
    name: "al"
  },
  {
    age: 10,
    name: "ben"
  },
  {
    age: 15,
    name: "mac"
  },
  {
    age: 15,
    name: "sarah"
  },
  {
    age: 20,
    name: "jack"
  },
  {
    age: 24,
    name: "dan"
  }
];

const age = [15, 23];

function lowerAndUpperBound(value) {
  let lower = { age: -1 };
  let upper = { age: 9999 };

  for (const obj of data) {
    if (obj.age === value && lower.age !== value) {
      lower = obj;
    }
    if (obj.age === value && upper.age !== value) {
      upper = obj;
    }
    if (obj.age > lower.age && obj.age < value && lower.age !== value) {
      lower = obj;
    }
    if (obj.age < upper.age && obj.age > value && upper.age !== value) {
      upper = obj;
    }
  }
  return [lower, upper];
}

const result = age.map((i) => lowerAndUpperBound(i));

console.log(result);
about 4 years ago · Juan Pablo Isaza Report

0

Just write an if statement to check if the length of 'filtered' is 0 or not
Also use let instead of Var

function int(d, a){
    a = a.sort();
    //Basically same thing u wrote
    let filtered = d.filter((e)=>age.includes(e.age));
    if (filtered.length == 0) {
        //Assumes the length of "a" is 2
        return d.filter((e)=> e.age >= a[0] && e.age <= a[1])
    } else {
        //If there is a result, return it
        return filtered
    }
}
let date1 = [{age:5, name:"al"},
              {age:10, name:"ben"},
              {age:15, name:"mac"},
              {age:15, name:"sarah"},
              {age:20, name:"jack"},
              {age:24, name:"dan"}
             ];
let date2 = [{age:5, name:"al"},
              {age:10, name:"ben"},
              {age:20, name:"jack"},
              {age:24, name:"dan"}
             ];

let age = [15, 23];
console.log(int(date1, age));
// Will result in this, has exact match:
/*
[
    {
        "age": 15,
        "name": "mac"
    },
    {
        "age": 15,
        "name": "sarah"
    }
]
*/
console.log(int(date2, age));
//Will result in this, has no exact match
/*
[
    {
        "age": 20,
        "name": "jack"
    }
]
*/
about 4 years ago · Juan Pablo Isaza Report

0

Try this, this is very easy to understand .

var date = [{age:5, name:"al"},

{age:10, name:"ben"},

{ age:15, name:"mac"},

{age:15, name:"sarah"},

{age:20, name:"jack"},

{age:24, name:"dan"} ];

var age = [15, 23];

const ans = date.filter(val=>age.includes(val.age)) ;

console.log(ans)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!