I need to know the best method to check if the value is in the array before push it
I have an array :
coordinates: [
{x: 160, y: 160},
{x: 192, y: 160},
{x: 224, y: 160},
{x: 224, y: 192},
];
For each call of my function , I need to check if new coordinates is it before proceed to push.
const payload= {x: 224, y: 190}
//Next function call
const payload = {x: 224, y: 190} // need to refuse pushing this value
I tried filter and other JS methods without success.
for example:
coordinates.filter(c => {if(payload !== c){coordinates.push(new)});
This one doesn't work for me .
since each coordinate is an object, you'll need to do a deep comparison to check if the object exists in your array already:
const coordinates = [{
x: 160,
y: 160
}, {
x: 192,
y: 160
}, {
x: 224,
y: 160
}, {
x: 224,
y: 192
}];
const isInArray = newCoordinate => coordinates.filter(c => newCoordinate.x === c.x && newCoordinate.y === c.y).length > 0;
console.log(isInArray({
x: 0,
y: 0
})) // false
console.log(isInArray({
x: 160,
y: 160
})) // true
I hope this helps
var coordinates= [
{x: 160, y: 160},
{x: 192, y: 160},
{x: 224, y: 160},
{x: 224, y: 192},
];
const payload = {x: 225, y: 193};
for(let prop of coordinates){
if(prop.x == payload.x || prop.y == payload.y){
var includes = true;
} else {
includes = false;
}
}
if(includes == false){
coordinates.push(payload);
}
console.log(coordinates);
A simple checker function to check duplicate objects
var coordinates=[
{x: 160, y: 160},
{x: 192, y: 160},
{x: 224, y: 160},
{x: 224, y: 192},
];
const pushCoordinate=(obj)=>{
var flag=0;
coordinates.forEach((elem)=>{
if(obj.x===elem.x && obj.y===elem.y){
flag=1;
}
});
if(flag===1){
return false;
}else{
return true;
}
};
var payload1 = {x: 224, y: 190};
var payload2 = {x:224, y:190};
if(pushCoordinate(payload1)){
coordinates.push(payload1);
}
if(pushCoordinate(payload2)){
coordinates.push(payload2);
}
console.log(coordinates);
you can simply use filter array method to check x,y values
and if the filtered array has no length then there are no matching values and push payload object
let filteredArr;
let coordinates = [
{x: 160, y: 160},
{x: 192, y: 160},
{x: 224, y: 160},
{x: 224, y: 192}
];
// checker fn
const checker = (obj) => filteredArr = coordinates.filter(elm => obj.x == elm.x && obj.y == elm.y);
let payload = {x: 224, y: 190};
checker(payload);
// no matching
if (filteredArr.length === 0) coordinates.push(payload);
console.log(coordinates);
var payload = {x: 224, y: 190};
var coordinates = [
{x: 160, y: 160},
{x: 192, y: 160},
{x: 224, y: 160},
{x: 224, y: 192},
];
var filtered = coordinates.filter(c => c.x == payload.x &&
c.y == payload.y);
if (filtered.length == 0) filtered.push(payload)
console.log("Filtered:", filtered)
You can use some() to check whether coordinates array includes the values of payload object
const coordinates = [
{x: 160, y: 160},
{x: 192, y: 160},
{x: 224, y: 160},
{x: 224, y: 192},
];
const payload = {x: 224, y: 192};
if(!coordinates.some(({x, y}) => x === payload.x && y === payload.y)) {
coordinates.push(payload);
}
console.log(coordinates);
Related
I have 1D pointer array like below:
Darray = [{x: 334, y: 400.5}, {x: 237, y: 389},{x: 149, y: 387.5},{x: 55, y: 379.5},{x: 210, y: 301.5},{x: 48, y: 295.5},{x: 378.5, y: 224.5},{x: 283, y: 217.5},{x: 121.5, y: 211.5},{x: 198.5, y: 211.5},{x: 42.5, y: 201},{x: 33, y: 134},{x: 364, y: 142},{x: 268.5, y: 137},{x: 192, y: 136.5},{x: 106, y: 131.5},{x: 263.5, y: 68},{x: 182.5, y: 63.5},{x: 102.5, y: 61.5},{x: 344.5, y: 65.5},{x: 32, y: 52}]
//console.log(Darray)
const points = Darray.slice();
points.sort((a, b) => a.y - b.y);
console.log(points)
I want to convert this 1D array into 2D array based on the array row length. For example,
row_length = [5, 5, 5, 2, 4]
new_Darray = [[element[1,1],element[1,2], element[1,3], element[1,4], element[1,5],
[element[2,1],element[2,2], element[2,3], element[2,4], element[2,5],
[element[3,1],element[3,2], element[3,3], element[3,4], element[3,5],
[element[4,1],element[4,2],
[element[5,1],element[5,2], element[5,3], element[5,4], element[5,5]]
Here, element[i] represents {x:someValue, y:someValue}
Sorry, if I wrongly represented anything. Any help will be highly appreciated.
Using map, slice and temp variable, can be simplified to one-liner
const Darray = [
{ x: 334, y: 400.5 },
{ x: 237, y: 389 },
{ x: 149, y: 387.5 },
{ x: 55, y: 379.5 },
{ x: 210, y: 301.5 },
{ x: 48, y: 295.5 },
{ x: 378.5, y: 224.5 },
{ x: 283, y: 217.5 },
{ x: 121.5, y: 211.5 },
{ x: 198.5, y: 211.5 },
{ x: 42.5, y: 201 },
{ x: 33, y: 134 },
{ x: 364, y: 142 },
{ x: 268.5, y: 137 },
{ x: 192, y: 136.5 },
{ x: 106, y: 131.5 },
{ x: 263.5, y: 68 },
{ x: 182.5, y: 63.5 },
{ x: 102.5, y: 61.5 },
{ x: 344.5, y: 65.5 },
{ x: 32, y: 52 },
];
const points = Darray.slice();
points.sort((a, b) => a.y - b.y);
// console.log(points);
const row_length = [5, 5, 5, 2, 4];
let last = 0;
const output = row_length.map((len) => points.slice(last, (last += len)));
console.log(output);
I have a data array like this
{x: 0, y: 7.9}
{x: 1, y: 7.5}
{x: 2, y: 7.0}
{x: 3, y: 7.4}
{x: 4, y: 7.3}
{x: 5, y: 7.2}
{x: 6, y: 7.5}
{x: 7, y: 7.6}
{x: 8, y: 7.7}
{x: 9, y: 7.2}
Based on this data, how can I find out the following y?
For example, I used a library to find out this data but how could I predict based on this data
the following data
eg for the next 30 indexes
now I have this
const cleanData = helpers.cleanData(this.data.datasets[0].data);
const ordersRegression = regression.linear(cleanData);
const regressionPoints = ordersRegression.points.map(([x, y]) => {
return {x, y};
});
this.data.datasets[1].data = regressionPoints;
But now I would like to do something with the data that I have to predict the following
It's possible ?
Is there a genuine formula?
This question already has answers here:
How to remove duplicates objects in array based on 2 properties?
(6 answers)
Closed 1 year ago.
I'm storing some coordinates in an array. It looks like this:
const coords = [{x: 260, y: 60}, {x: 180, y: 0}, {x: 180, y: 240}, {x: 360, y: 120}, {x: 180, y: 60}, {x: 180, y: 60}, {x: 180, y: 60}]
How can I filter this array so the objects are unique, meaning there are no duplicates of objects with same x and y value? Expected output should be:
const coords = [{x: 260, y: 60}, {x: 180, y: 0}, {x: 180, y: 240}, {x: 360, y: 120}, {x: 180, y: 60}]
I've seen some similar solutions, but they didn't really solve this problem.
I started with the following function
const output = Object.values(
coords.reduce( (c, e) => {
if (!c[e.x]) c[e.x] = e;
return c;
}, {})
but it only returns objects with different x values, so it just completely ommits y value.
One idea is to use a Set, map the x & y into a string, and then deserialize the Set to have unique x,y's..
eg..
const coords = [{x: 260, y: 60}, {x: 180, y: 0}, {x: 180, y: 240}, {x: 360, y: 120}, {x: 180, y: 60}, {x: 180, y: 60}, {x: 180, y: 60}];
const dedup = [...new Set(coords.map(m => `${m.x}:${m.y}`))].map(m => {
const [x,y] = m.split(':').map(n => n | 0);
return {x,y};
});
console.log(dedup);
We can use Array.reduce(),
along with a Map to get the required result.
We'd add each item to the map, using the concatenated x and y values as keys, then return the values() to get de-duplicated values.
This will have complexity of O(n), so it will be efficient for large arrays.
const coords = [{x: 260, y: 60}, {x: 180, y: 0}, {x: 180, y: 240}, {x: 360, y: 120}, {x: 180, y: 60}, {x: 180, y: 60}, {x: 180, y: 60}];
const dedup = [...coords.reduce((map, { x, y }) => {
return (map.set(`${x}-${y}`, { x, y }));
}, new Map()).values()];
console.log('De-duplicated:', dedup)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Or with a regular object:
const coords = [{x: 260, y: 60}, {x: 180, y: 0}, {x: 180, y: 240}, {x: 360, y: 120}, {x: 180, y: 60}, {x: 180, y: 60}, {x: 180, y: 60}];
const dedup = Object.values(coords.reduce((acc, { x, y }) => {
return { ...acc, [`${x}-${y}`]: { x, y }}
}, {}));
console.log('De-duplicated:', dedup)
.as-console-wrapper { max-height: 100% !important; top: 0; }
A pretty inefficient (O(n^2)), but flexible and straightforward solution: You first define a function that checks if two coordinates are equal. Then you filter all elements which have an equal element at a later position in the array.
const coords = [{x: 260, y: 60}, {x: 180, y: 0}, {x: 180, y: 240}, {x: 360, y: 120}, {x: 180, y: 60}, {x: 180, y: 60}, {x: 180, y: 60}]
const customUnique = (arr, isEqual) => {
// filter elements where an equal element exists at an earlier position
// thus the first element is kept
return arr.filter((a, i) => !arr.some((b, j) => i > j && isEqual(a, b)))
}
console.log(customUnique(coords, (a, b) => a.x === b.x && a.y === b.y))
You can use originalArray.reduce() with an array instead of an object, so you can make use of array.find.
const coords = [{x: 260, y: 60}, {x: 180, y: 0}, {x: 180, y: 240}, {x: 360, y: 120}, {x: 180, y: 60}, {x: 180, y: 60}, {x: 180, y: 60}]
console.log(
coords.reduce((arr, e) => {
if (!arr.find(item => item.x == e.x && item.y == e.y)) {
arr.push(e);
}
return arr;
}, [])
);
Yet another simple solution using a temporary array. However not the best as I could say:
const filteredCoords: any = [];
for(let coord of coords)
if (!filteredCoords.find((ele: { x: number; y: number; }) => ele.x == coord.x && ele.y == coord.y)){
filteredCoords.push(coord)
}
I need to find 4 values in this order in a array of object:
lowest x & y
highest x and lowest y
highest x & y
highest y & lowest x
my object is like this :
object: [
{id: 1, x: 160, y: 160},
{id: 2, x: 192, y: 160},
{id: 3, x: 224, y: 160},
{id: 4, x: 224, y: 192},
{id: 5, x: 192, y: 192},
{id: 6, x: 160, y: 192},
{id: 7, x: 160, y: 224},
{id: 8, x: 192, y: 224},
{id: 9, x: 224, y: 224}
],
the result must be an array with those 4 object .
Thanks
This is not the complete answer; highest x and lowest y and vice versa remained but it might give other contributors some insights how to solve the problem:
const object = [
{id: 9, x: 224, y: 224},
{id: 2, x: 192, y: 160},
{id: 3, x: 224, y: 160},
{id: 4, x: 224, y: 192},
{id: 5, x: 192, y: 192},
{id: 6, x: 160, y: 192},
{id: 7, x: 160, y: 224},
{id: 8, x: 192, y: 224},
{id: 1, x: 160, y: 160},
{id: 9, x: 224, y: 224}
];
object.sort((objA, objB) => (objA.x * objA.y) - (objB.x * objB.y));
const HighestXAndHighestY = object[object.length-1];
const lowestXAndlowestY = object[0];
console.log(HighestXAndHighestY)
console.log(lowestXAndlowestY)
Sorry if the title is misleading, had no idea on how to title it.
Here's a var :
var rects = [
{x: 32, y: 32, w: 32, h: 32},
{x: 32, y: 32, w: 32, h: 32},
{x: 0, y: 0, w: 32, h: 32}
], i = 0, r;
Now, I want to do something like this :
for (nb = 0; nb > 10; nb++;) {
var rects = [ {x: 32 * nb, y: 32 * nb, w: 32, h: 32} ], i = 0, r;
}
(Of course this doesn't work).
How can I do this ? Thanks !
Either use Array.from :
let rects = Array.from({length:3} , _=>({x: 32, y: 32, w: 32, h: 32})), i = 0, r;
Or use a traditional for loop:
let rects = [], i = 0, r;
for(let index = 0; index < 3; index++)
rects[index] = {x: 32, y: 32, w: 32, h: 32};