im looking for a function to add the value of the previous day to each next day.
I have following array:
{x: "8.9.2021", y: 0},
{x: "9.9.2021", y: 33},
{x: "10.9.2021", y: 0},
{x: "11.9.2021", y: 0},
{x: "12.9.2021", y: 0},
{x: "13.9.2021", y: 0},
{x: "14.9.2021", y: 0},
{x: "15.9.2021", y: 8},
{x: "16.9.2021", y: 0},
{x: "17.9.2021", y: 99},
{x: "18.9.2021", y: 0},
{x: "19.9.2021", y: 0},
{x: "20.9.2021", y: 113},
{x: "21.9.2021", y: 57},
{x: "22.9.2021", y: 16},
{x: "23.9.2021", y: 0},
...
And im looking for something like this:
{x: "8.9.2021", y: 0},
{x: "9.9.2021", y: 33},
{x: "10.9.2021", y: 33},
{x: "11.9.2021", y: 33},
{x: "12.9.2021", y: 33},
{x: "13.9.2021", y: 33},
{x: "14.9.2021", y: 33},
{x: "15.9.2021", y: 41},
{x: "16.9.2021", y: 41},
{x: "17.9.2021", y: 140},
{x: "18.9.2021", y: 140},
{x: "19.9.2021", y: 140},
{x: "20.9.2021", y: 253},
{x: "21.9.2021", y: 310},
{x: "22.9.2021", y: 326},
{x: "23.9.2021", y: 326},
...
That is what i tried but that froze my page
const add = (array) => {
let newArray= orders;
for (let i = 0; i < array.length; i++) {
newArray[i] = { x: orders[i].x, y: orders[i].y + orders[--i] && orders[--i].y, }; }
return newArray;
};
Didn't find any solutions yet. The solution should be as simple as possible.
You can iterate over the array, and add the values of y
const data = [{x: "8.9.2021", y: 0},
{x: "9.9.2021", y: 33},
{x: "10.9.2021", y: 0},
{x: "11.9.2021", y: 0},
{x: "12.9.2021", y: 0},
{x: "13.9.2021", y: 0},
{x: "14.9.2021", y: 0},
{x: "15.9.2021", y: 8},
{x: "16.9.2021", y: 0},
{x: "17.9.2021", y: 99},
{x: "18.9.2021", y: 0},
{x: "19.9.2021", y: 0},
{x: "20.9.2021", y: 113},
{x: "21.9.2021", y: 57},
{x: "22.9.2021", y: 16},
{x: "23.9.2021", y: 0}]
const incremented = []
data.forEach((el, index) => {
index === 0 ? incremented.push(el) :
incremented.push({...el, y: el.y + incremented[index-1].y})
})
console.log(incremented)
Related
This is the array. I want to filter the closet x and y coordinates from a target which is:
target.x = 310 and target.y = 280;
the correct value is the value in the array at number 8.
I just a filter scheme to filter the correct value from the array that's closest to the target x and y?
distFromBall = [
{y: 10, x: 300, number: 1},
{y: 23.333333333333314, x: 75, number: 2},
{y: 23.333333333333314, x: 225, number: 3},
{y: 23.333333333333314, x: 375, number: 4},
{y: 23.333333333333314, x: 525, number: 5},
{y: 270, x: 75, number: 6},
{y: 270, x: 225, number: 7},
{y: 270, x: 375, number: 8},
{y: 270, x: 525, number: 9},
{y: 290, x: 150, number: 10},
{y: 290, x: 450, number: 11}]
i tried something like this but no luck:
var f = distFromBall.filter(function (a,i) {
return a.x > 0;
});
f.sort(function (a, b) {
return a.x - b.x;
});
f.sort(function (a, b) {
return a.y - b.y;
});
var num = distFromBall[0].number;
var pl = arrayPlayerHome.find(function (a) {
return a.shirtNumber == num;
});
return pl;
With the pythagorean theorem and .reduce, you can keep the closest number object to the target as the accumulator and return it at the end.
const getDist = (a, b) => Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
const findClosest = (target) => distFromBall
.reduce((a, item) => getDist(item, target) < getDist(a, target) ? item : a);
const distFromBall = [
{y: 10, x: 300, number: 1},
{y: 23.333333333333314, x: 75, number: 2},
{y: 23.333333333333314, x: 225, number: 3},
{y: 23.333333333333314, x: 375, number: 4},
{y: 23.333333333333314, x: 525, number: 5},
{y: 270, x: 75, number: 6},
{y: 270, x: 225, number: 7},
{y: 270, x: 375, number: 8},
{y: 270, x: 525, number: 9},
{y: 290, x: 150, number: 10},
{y: 290, x: 450, number: 11}]
console.log(findClosest({ x: 310, y: 280 }));
I have an array containing hundreds sometimes thousands of objects with vectors of 3D objects. There are three or even more identical objects in the array (I think they are needed also for the render to know which side the normals of the surface are facing) but for what I want to do I need the identical objects gone. The bad part is, I cant just check one value since sometimes two objects share for example the same value for x but then the y or z is different. Is there an efficient way to do that? Unfortunately all tutorials I found deal with checking for one value and I need to check all of them.
const vectors = [
{x: 6.869495194905539e-9, y: -0.11905603855848312, z: -0.3318425416946411},
{x: 6.869495194905539e-9, y: -0.11905603855848312, z: -0.3318425416946411},
{x: 6.869495194905539e-9, y: -0.11905603855848312, z: -0.3318425416946411},
{x: 0.06476999074220657, y: -0.11905603855848312, z: -0.3254662752151489},
{x: 0.06476999074220657, y: -0.11905603855848312, z: -0.3254662752151489},
{x: 0.06476999074220657, y: -0.11905603855848312, z: -0.3254662752151489},
{x: 0.06476999074220657, y: -0.11905603855848312, z: -0.3254662752151489},
{x: 0.12705090641975403, y: -0.11905603855848312, z: -0.306582510471344},
{x: 0.12705090641975403, y: -0.11905603855848312, z: -0.306582510471344},
{x: 0.12705090641975403, y: -0.11905603855848312, z: -0.306582510471344},
etc
etc
]
You can use sets to remove the duplicates:
const vectors = [
{x: 6.869495194905539e-9, y: -0.11905603855848312, z: -0.3318425416946411},
{x: 6.869495194905539e-9, y: -0.11905603855848312, z: -0.3318425416946411},
{x: 6.869495194905539e-9, y: -0.11905603855848312, z: -0.3318425416946411},
{x: 0.06476999074220657, y: -0.11905603855848312, z: -0.3254662752151489},
{x: 0.06476999074220657, y: -0.11905603855848312, z: -0.3254662752151489},
{x: 0.06476999074220657, y: -0.11905603855848312, z: -0.3254662752151489},
{x: 0.06476999074220657, y: -0.11905603855848312, z: -0.3254662752151489},
{x: 0.12705090641975403, y: -0.11905603855848312, z: -0.306582510471344},
{x: 0.12705090641975403, y: -0.11905603855848312, z: -0.306582510471344},
{x: 0.12705090641975403, y: -0.11905603855848312, z: -0.306582510471344}
];
const arrayWithoutDuplicates = Array.from(
new Set(vectors.map(v => JSON.stringify(v))),
json => JSON.parse(json)
);
Nevermind, I did finally find an answer in another thread here.
var result = arr.reduce((unique, o) => {
if(!unique.some(obj => obj.x === o.x && obj.y === o.y && obj.z === o.z)) {
unique.push(o);
}
return unique;
},[]);
console.log(result);
Remove duplicate values from an array of objects in javascript
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 have an array of objects like this
const ArrayOfObject = [
{x: "1622078100000", y: 1},
{x: "1622010000000", y: 1},
{x: "1622009940000", y: 6},
{x: "1622009880000", y: 4},
{x: "1622009820000", y: 2},
{x: "1622073600000", y: 1}
]
I want something like this
const ArrayOfObject = [
{x: 1622078100000, y: 1},
{x: 1622010000000, y: 1},
{x: 1622009940000, y: 6},
{x: 1622009880000, y: 4},
{x: 1622009820000, y: 2},
{x: 1622073600000, y: 1}
]
parse the x prop from string to number. How to accomplish this in Javascript ES6 or ES5?
There is no special function to do this. You literally have to map over them.
Better instead to leave them as is, and only do it when needed
const ArrayOfObject = [
{x: "1622078100000", y: 1},
{x: "1622010000000", y: 1},
{x: "1622009940000", y: 6},
{x: "1622009880000", y: 4},
{x: "1622009820000", y: 2},
{x: "1622073600000", y: 1}
]
const ArrayOfObjectUpdated = ArrayOfObject.map(item => ({...item, x: Number(item.x)}))
You can use the + unary operator to convert a string to a number, like so:
const newArr = [];
ArrayOfObject.forEach(obj => newArr.push({x: +obj.x, y: obj.y}));
see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus
Due to the great support from SO users on my initial issue here I could transfer the suggested solution to my certain project environment and could -almost- make it happen.
I have a returned array object resultVolume with the following structure:
0: {x: "AUDUSD", y: 680, count: 10}
1: {x: "EURCAD", y: 690, count: 9}
2: {x: "USDCAD", y: 250, count: 8}
3: {x: "EURHUF", y: 600, count: 8}
4: {x: "CADCHF", y: 560, count: 7}
5: {x: "AUDNZD", y: 320, count: 7}
6: {x: "AUDCHF", y: 330, count: 7}
7: {x: "EURNOK", y: 590, count: 7}
8: {x: "EURJPY", y: 70, count: 7}
9: {x: "EURAUD", y: 430, count: 6}
10: {x: "EURSGD", y: 50, count: 5}
11: {x: "EURCHF", y: 50, count: 5}
12: {x: "GBPUSD", y: 370, count: 4}
13: {x: "AUDJPY", y: 80, count: 4}
14: {x: "CHFJPY", y: 240, count: 4}
15: {x: "USDJPY", y: 170, count: 4}
16: {x: "SGDJPY", y: 40, count: 4}
17: {x: "AUDCAD", y: 40, count: 4}
I now would like to do both
cut all objects after the 5th one (so after object number 4) and
remove the count property from all of the objects.
I tried the following:
delete resultCount.count;
const finalResultCount = resultCount.slice(0, 5);
which returns
0: {x: "AUDUSD", y: 680, count: 10}
1: {x: "EURCAD", y: 690, count: 9}
2: {x: "USDCAD", y: 250, count: 8}
3: {x: "EURHUF", y: 600, count: 8}
4: {x: "CADCHF", y: 560, count: 7}
5: {x: "AUDNZD", y: 320, count: 7}
with the count property not being removed.
Following these solutions I am not sure how to access each object since they all have different numbers in front. So probably I need a loop to achieve the desired outcome?
let finalResultCount = resultCount.slice(0, 5);
finalResultCount.forEach(item => delete item.count);
const arr = [{x: "AUDUSD", y: 680, count: 10},
{x: "EURCAD", y: 690, count: 9},
{x: "USDCAD", y: 250, count: 8},
{x: "EURHUF", y: 600, count: 8},
{x: "CADCHF", y: 560, count: 7},
{x: "AUDNZD", y: 320, count: 7}]
arr.forEach((item, index, array) => delete array[index].count);
console.log(arr);
var arr = [{
x: "AUDUSD",
y: 680,
count: 10
}, {
x: "EURCAD",
y: 690,
count: 9
}, {
x: "USDCAD",
y: 250,
count: 8
}, {
x: "EURHUF",
y: 600,
count: 8
}, {
x: "CADCHF",
y: 560,
count: 7
}, {
x: "AUDNZD",
y: 320,
count: 7
}, {
x: "AUDCHF",
y: 330,
count: 7
}, {
x: "EURNOK",
y: 590,
count: 7
}, {
x: "EURJPY",
y: 70,
count: 7
}, {
x: "EURAUD",
y: 430,
count: 6
}, {
x: "EURSGD",
y: 50,
count: 5
}, {
x: "EURCHF",
y: 50,
count: 5
}, {
x: "GBPUSD",
y: 370,
count: 4
}, {
x: "AUDJPY",
y: 80,
count: 4
}, {
x: "CHFJPY",
y: 240,
count: 4
}, {
x: "USDJPY",
y: 170,
count: 4
}, {
x: "SGDJPY",
y: 40,
count: 4
}, {
x: "AUDCAD",
y: 40,
count: 4
}];
var newArr = arr.slice(0, 5).map(e => {
delete e.count;
return e;
});
console.log(newArr);
var resultVolume = [
{x: "AUDUSD", y: 680, count: 10},
{x: "EURCAD", y: 690, count: 9},
{x: "USDCAD", y: 250, count: 8},
{x: "EURHUF", y: 600, count: 8},
{x: "CADCHF", y: 560, count: 7},
{x: "AUDNZD", y: 320, count: 7},
{x: "AUDCHF", y: 330, count: 7},
{x: "EURNOK", y: 590, count: 7},
{x: "EURJPY", y: 70, count: 7},
{x: "EURAUD", y: 430, count: 6},
{x: "EURSGD", y: 50, count: 5},
{x: "EURCHF", y: 50, count: 5},
{x: "GBPUSD", y: 370, count: 4},
{x: "AUDJPY", y: 80, count: 4},
{x: "CHFJPY", y: 240, count: 4},
{x: "USDJPY", y: 170, count: 4},
{x: "SGDJPY", y: 40, count: 4},
{x: "AUDCAD", y: 40, count: 4},
]
delete resultVolume.splice(5);
resultVolume.forEach(item => delete item.count);
console.log(resultVolume);