How to reverse all arrays inside an object - javascript

var data = {
id: 1,
track: {
"1": [
{x: 10, y: 10},
{x: 11, y: 11},
{x: 12, y: 12}
],
"2": [
{x: 10, y: 10},
{x: 11, y: 11},
{x: 12, y: 12}
]
}
}
console.log(data.track);
var rev = data.track["1"].reverse();
console.log(rev);
How can i reverse every array inside "track" object? But I showed you above, that i am able to reverse array, by selecting it by key, but can i literally reverse every array inside "track" object?

Use Object.keys() to find all keys in your data structure
var data = {
id: 1,
track: {
"1": [
{x: 10, y: 10},
{x: 11, y: 11},
{x: 12, y: 12}
],
"2": [
{x: 10, y: 10},
{x: 11, y: 11},
{x: 12, y: 12}
]
}
}
var keys = Object.keys(data.track);
var count = keys.length;
for (var i=0;i<count;i++)
{
var rev = data.track[keys[i]].reverse();
console.log(rev);
}

It's simple. Just loop the data.track to get reverse result.
for (var i in data.track) {
console.log(data.track[i].reverse());
}

Related

how to fetch distinct objects in array : Javascript

I am trying to fetch unique objects from an array which may have duplicate objects. I have tried new Set and new Map but i still haven't gotten my result.
For example i have the following array of objects
const myArray = [{ x: 10, y: 22}, { x: 11, y: 22}, { x: 12, y: 22}, { x: 12, y: 22}, { x: 12, y: 23}];
console.log([...new Set(myArray.map((item) => item.x && item.y ))]) // [22, 23]
when i want this
[{ x: 10, y: 22}, { x: 11, y: 22}, { x: 12, y: 22}, { x: 12, y: 23}];
it should remove the fourth object in myArray, since it is repeating
You can use reduce for that along with some:
const myArray = [{ x: 10, y: 22}, { x: 11, y: 22}, { x: 12, y: 22}, { x: 12, y: 22}, { x: 12, y: 23}];
const Filtered = [];
const filterDuplicates = myArray.reduce((arr, el) => {
if(!arr.some(current => current.x === el.x && current.y === el.y)) {
arr.push(el);
}
return arr;
}, Filtered);
console.log(Filtered);
What your map returns as follows
myArray.map((item) => item.x && item.y) // [ 22, 22, 22, 22, 23 ]
because it first check wheather the item.x is truthy or not. As it true always so it returns the value after &&
and when you apply the set, It will filter the unique value from the array
[...new Set(myArray.map((item) => item.x && item.y))] // [ 22, 23 ]
Alternate apprach
const myArray = [
{ x: 10, y: 22 },
{ x: 11, y: 22 },
{ x: 12, y: 22 },
{ x: 12, y: 22 },
{ x: 12, y: 23 },
];
const strArray = myArray.map(({ x, y }) => `${x}/${y}`);
const str = [...new Set(strArray)];
const result = str.map((str) => {
const [x, y] = str.split("/");
return { x, y };
});
console.log(result);

Get the id of the nearest coordinate object

I am using the answer in the example to find the nearest object coordinate. How do I find the index of the nearest point from var points if e.g. the nearest point is {x: 12, y: 18}, then index = 1?
I want to avoid using an extra indexOf() if that step can be saved.The code is:
var points = [
{x: 10, y: 20},
{x: 12, y: 18},
{x: 20, y: 30},
{x: 5, y: 40},
{x: 100, y: 2}
];
function d(point) {
return Math.pow(point.x, 2) + Math.pow(point.y, 2);
}
var closest = points.slice(1).reduce(function(min, p) {
if (d(p) < min.d) min.point = p;
return min;
}, {point: points[0], d:d(points[0])}).point;
console.log(closest);
You could map the array to an array of objects first that also contain the index:
const points = [
{x: 10, y: 20},
{x: 12, y: 18},
{x: 20, y: 30},
{x: 5, y: 40},
{x: 100, y: 2}
];
const distance = (point) => {
return Math.pow(point.x, 2) + Math.pow(point.y, 2);
}
const result = points
.map((point, index) => ({ point, index }))
.reduce((a, b) => distance(a.point) < distance(b.point) ? a : b);
console.log(result);

How to update values not present in one array of objects from another array of objects?

I have two arrays of objects which contain a huge amount of data.
The structure of these two arrays goes something like this.
arr1 = [
{x: 1, y: '2018-01-01'},
{x: 2, y: '2018-01-02'},
{x: 3, y: '2018-01-03'},
{x: 5, y: '2018-01-05'},
....
]
arr2 = [
{x: 1, y: '2018-01-01'},
{x: 2, y: '2018-01-02'},
{x: 3, y: '2018-01-03'},
{x: 4, y: '2018-01-04'},
{x: 5, y: '2018-01-05'},
{x: 6, y: '2018-01-08'}
]
I want to update arr2 in such a way that it updates the array of objects with values that are only present in arr1 and drop any values not present in arr1. Note, I want to update the original arr2 and not return a new array.
I tried iterating through individual arrays and remove values not present but not luck.
You could get a map and iterate from the end for splicing unknown items or update changed values.
var arr1 = [{ x: 1, y: '2018-01-01x' }, { x: 2, y: '2018-01-02' }, { x: 3, y: '2018-01-03' }, { x: 5, y: '2018-01-05' }],
arr2 = [{ x: 1, y: '2018-01-01' }, { x: 2, y: '2018-01-02' }, { x: 3, y: '2018-01-03' }, { x: 4, y: '2018-01-04' }, { x: 5, y: '2018-01-05' }, { x: 6, y: '2018-01-08' }],
map = arr1.reduce((m, { x, y }) => m.set(x, y), new Map),
i = arr2.length;
while (i--) {
if (map.has(arr2[i].x)) {
if (map.get(arr2[i].x) !== arr2[i].y) {
arr2[i].y = map.get(arr2[i].x);
}
} else {
arr2.splice(i, 1);
}
}
console.log(arr2);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Is it possible to search a object in an array with both of its properties

I have the following code
const xPosition = coordinates.find(position => position.x === avoidObstacleX);
This returns me the coordinates {x: 26, y: 10} this is not wrong, but I have another coordinate that is the one I will like to output which is {x: 26, y: 11} Is there a way I can pass two parameters to the find method?
You could use two variables (not parameters to the find method itself), like you already use one:
function findObstacle(coordinates, avoidObstacleX, avoidObstacleY) {
return coordinates.find(position => position.x === avoidObstacleX
&& position.y === avoidObstacleY);
}
const xyPosition = findObstacle(coordinates, avoidObstacleX, avoidObstacleY);
But from the other answer I now learn that there are two interpretations of your question...
find only retrieves a single element, you need to use the filter method:
const coordinates = [ {x: 26, y: 10}, {x: 26, y: 11}, {x: 12, y: 34} ]
const avoidObstacleX = 26;
// returns [ {x: 26, y: 10}, {x: 26, y: 11} ]
const xPosition = coordinates.filter(position => position.x === avoidObstacleX);
To pass one value:
const coordinates= [
{x: 26, y: 10},
{x: 26, y: 11},
{x: 36, y: 6},
{x: 7,y: 8}
]
const avoidObstacleX=26;
let result = coordinates.filter(position=> {
return position.x === avoidObstacleX ;
})
console.log(result)
You can pass two values:
const coordinates= [
{x: 26, y: 11},
{x: 26, y: 11},
{x: 26, y: 11},
{x: 7,y: 8}
]
function find(avoidObstaclex,avoidObstacley){
let result= coordinates.filter(position=> {
return position.x === avoidObstaclex && position.y === avoidObstacley ;
})
return result;}
const avoidObstacleX=26;
const avoidObstacleY=11;
console.log(find(avoidObstacleX,avoidObstacleY))

Convert array of objects into array of primitives (extracted from object properties)

Consider the following code:
var input = [{x: 1, y: 6}, {x: 4, y: 3}, {x: 9, y: 2}];
var output = convert(input);
console.log(output); // = [1, 6, 4, 3, 9, 2]
What is the shortest, most concise convert function I can write that will give me the output shown?
So far I've come up with the following:
function convert(input) {
var output = [];
input.forEach(function(obj) {
output.push(obj.x, obj.y);
});
return output;
}
But surely there's a nice one-liner way of doing this?
With Array.prototype.reduce method it will save you two lines of code:
function convert(arr) {
return arr.reduce(function(prev, curr) {
return prev.concat(curr.x, curr.y);
}, []);
}
var input = [{x: 1, y: 6}, {x: 4, y: 3}, {x: 9, y: 2}];
document.write(JSON.stringify( convert(input) ));

Categories

Resources