简体   繁体   English

如何从JavaScript中的对象数组中获取最接近的先前ID

[英]How to get closest previous id from array of object in javascript

I have an array of object,I want to get the closest previous id from nearest object.I can able to get closest next id,its working fine but for previous is not working.Its taking direct first id of object.Here is the code below.Can anyone please help me on it. 我有一个对象数组,我想从最近的对象获取最近的前一个ID。我可以获取最近的下一个ID,它可以正常工作,但对于前一个不起作用。它直接获取对象的第一个ID。这是代码下面的任何人都可以帮助我。

JAVASCRIPT JAVASCRIPT

 const array = [{id:4}, {id:10}, {id:15}]; const findClosesPrevtId = (x) => ( array.find( ({id}) => x <= id ) || {} ).id; const findClosestNextId = (x) => ( array.find( ({id}) => x >= id ) || {} ).id; console.log(findClosesPrevtId(5)); console.log(findClosestNextId(11)); 

Cause x <= i will be fullfilled for the first element if you use find to search from left to right. 原因x <= i将fullfilled为第一要素,如果你使用find搜索由左到右。 Use findLast to search from right to left. 使用findLast从右到左搜索。

Unfortunately I just found out that there is actually not a findLast yet (there is reduceRight , lastIndexOf ... :/), so you have to write it yourself: 不幸的是,我发现实际上还没有findLast (存在reduceRightlastIndexOf ...:/),因此您必须自己编写:

  Object.defineProperty(Array.prototype, "findLast", {
    value(cb, context) {
       for(let i = this.length - 1; i >= 0; i--)
         if(cb.call(context, this[i], i, this))
            return this[i];
   }
 });

const findClosesPrevtId = (x) => ( array.findLast( ({id}) => x <= id ) || {} ).id;

I made some changes to your code and it should work now. 我对您的代码进行了一些更改,现在应该可以使用了。 Have a look. 看一看。

  const array = [{id:3}, {id:4}, {id:10}, {id:15}]; // you should order the list by id before you try to search, this incase you have not orginized list. // filter the list first and get the prev id to 5 // you should get 3 and 4 then // slice(-1) to get the last element of the array which should be 4 const findClosesPrevtId = (x) => (array.filter(({id}) => id <= x ).slice(-1)[0] || {}).id; const findClosestNextId = (x) => (array.filter(({id}) => id >= x )[0] || {}).id; console.log("Prev to 5:"+ findClosesPrevtId(5)); console.log("Next to 11:" +findClosestNextId(11)); 

You could store the precious/next element and stop the iteration if id is greater than the wanted id. 如果id大于所需的id,则可以存储贵重/ next元素并停止迭代。

 const closestPrevious = id => { var last = {}; array.some(o => o.id > id || (last = o, false)) return last.id; }, closestNext = id => { var next = array[0]; array.some((o, i, { [i + 1]: n = {} }) => o.id > id || (next = n, false)) return next.id; }, array = [{ id: 4 }, { id: 10 }, { id: 15 }]; console.log(closestNext(5)); // 10 console.log(closestNext(11)); // 15 console.log(closestPrevious(5)); // 4 console.log(closestPrevious(11)); // 10 

I find it easier to reverse the array and switch the comparison from >= to <= : 我发现更容易反转数组并将比较从>=切换到<=

 const findClosestNextId = (x, arr) => (arr.find ( ({id}) => id >= x) || {} ) .id const findClosestPrevId = (x, arr) => (arr .slice(0) .reverse() .find ( ({id}) => id <= x) || {}) .id const array = [{ id: 4 }, { id: 10 }, { id: 15 }]; console .log ( findClosestNextId (5, array), //=> 10 findClosestNextId (11, array), //=> 15 findClosestNextId (42, array), //=> undefined findClosestPrevId (5, array), //=> 4 findClosestPrevId (11, array), //=> 10 findClosestPrevId (2, array), //=> undefined ) 

The slice call is there to prevent this from modifying the original array. slice调用在那里防止其修改原始数组。 This will return undefined if there is no element found. 如果找不到元素,则将返回undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM