Home > Net >  Value in between javascript
Value in between javascript

Time:01-02

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.

CodePudding user response:

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 callback(obj) {
  if (obj.age >= age[0] && obj.age <= age[1]) {
    return true;
  }
}

const result = data.filter(callback);

console.log(result)

CodePudding user response:

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"
    }
]
*/

CodePudding user response:

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)

  • Related