Search element in array of objects - javascript

Could someone guide me how to find an element in an array?
I always get false for elements that exist.
Thank you.
arr = new Array;
arr.push({x: "a", y: 1});
arr.push({x: "b", y: 2});
arr.push({x: "c", y: 3});
​
elem = {x: "c", y: 3};
​
if (arr.includes(elem)){
console.log('true');
} else {
console.log('false');
}

You are looking for {x: "c", y: 3} object in arr and It will never be equal to any other object because {} !== {}.
Remember: Object are compared with reference but the primitives are checked by their value.
In your code you are checking an object reference with another reference, which is not equal so that's why it logs false.
If there are array of numbers then you should use includes as:
const arr = [10, 20, 30];
if(arr.includes(20)) console.log("true");
else console.log("false");
You can use find to find the element in the array
const arr = new Array();
arr.push({ x: "a", y: 1 });
arr.push({ x: "b", y: 2 });
arr.push({ x: "c", y: 3 });
const elem = { x: "c", y: 3 };
if (arr.find((o) => o.x === elem.x && o.y === elem.y)) {
console.log("true");
} else {
console.log("false");
}

since you are working with a 2d array i believe your object will be allways same for X and Y , so i suggest you to make a function like that for further use in your code
arr = new Array;
arr.push({x: "a", y: 1});
arr.push({x: "b", y: 2});
arr.push({x: "c", y: 3});
elem = {x: "c", y: 3};
console.log(findXY(arr,elem))
function findXY(arr,val){
return arr.find(each=>{
return each.x == val.x && each.y == val.y
})
}

Related

Find object within array of arrays and return the array it belongs to

I have the following array structure:
const array = [array1, array2, array3];
Each one of the three arrays consists of objects of form:
array1 = [{x: 0, y: 1}, {x: 5, y: 9}, {x: 1, y: 8}, {x: 3, y: 2}, etc]
I am trying to find the most efficient way to go through arrays of array and return the array to which a particular (unique) object belongs. For example I have object
{x:9, y:5}
which can be uniquely found in array2, so I want to return array2.
here's what I've tried:
const array = [array1, array2, array3];
for (let x = 0; x < array.length; x++) {
for (let y = 0; y < array[x].length; y++) {
array[x].find(e => e === array[x][y])
return array[x];
}
}
You'll need two loops, but you can use methods that do the iteration for you:
let array1 = [{x: 0, y: 1}, {x: 5, y: 9}, {x: 1, y: 8}, {x: 3, y: 2}];
let array2 = [{x: 5, y: 4}, {x: 4, y: 5}, {x: 8, y: 8}, {x: 3, y: 2}];
let array3 = [{x: 4, y: 3}, {x: 0, y: 6}, {x: 7, y: 8}, {x: 5, y: 2}];
const array = [array1, array2, array3];
let obj = array2[2]; // let's find this one...
let result = array.find(arr => arr.includes(obj));
console.log(result);
Here use find
data = [
[{x:1, y:2}, {x:2, y:3}],
[{x:3, y:2}, {x:4, y:3}],
[{x:5, y:2}, {x:6, y:3}],
[{x:7, y:2}, {x:8, y:3}]
];
const getArray = ({x, y}) => data.find(a => a.some(o => o.x === x && o.y === y));
console.log(getArray({x:3, y:2}));
TLDR; There is a working example in this fiddle
This can be accomplished using the following 3 things:
a library such as lodash to check for object equality (https://lodash.com/docs/4.17.15#isEqual)
The reason for this is that the behaviour of directly comparing two objects is different than you might think more info here
array.findIndex to find the index of the outer array (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)
array.find to find the element in an inner array (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
The following method findObjectInNestedArray will do what you'd like.
const findObjectArray = (obj, arr) => {
index = arr.findIndex(a => a.find(e => _.isEqual(e, obj)))
return arr[index] // will return `undefined` if not found
}
// Example code below
const array1 = [{x: 0, y: 1}, {x: 5, y: 9}, {x: 1, y: 8}, {x: 3, y: 2}];
const array2 = [{x: 1, y: 1}, {x: 2, y: 2}, {x: 3, y: 3}, {x: 4, y: 4}, {x:9, y:5}];
const array3 = [{x: 5, y: 5}];
const arrays = [array1, array2, array3];
const inArray2 = {x:9, y:5};
const notInAnyArray = {x:0, y:0};
console.log('array2', findObjectArray(inArray2, arrays));
console.log('not in array', findObjectArray(notInAnyArray, arrays));
I know I said a single iteration was impossible before, but I devised a possible method that could work under specific circumstances. Essentially, you can sort the properties then stringify the objects for instant lookups. The sort is necessary to ensure you always get a consistent stringified output regardless of the object's properties preexisting order. There are three caveats to this method:
the objects CANNOT contain functions. Properties with functions are dropped in the stringification process.
NaN and infinity are converted to null, which can cause unexpected "matches" in the cache
If the depth of the object is not known (i.e. the target objects can contain references to arrays and other objects), then you'll need to deeply traverse through every level before stringifying.
It's a trade-off that's only improves performance when comparing deeply nested or extremely large objects. It's scalable, though, I guess.. Here's an example of how it could be done:
// sort's an array's values, handling subarrays and objects with recursion
const sortArr = arr => arr.sort().map(el => typeof el === 'object' ? (Array.isArray(el) ? sortArr(el) : sortObj(el)) : el)
// sorts a key's objects, then recreates the object in a consistent order
const sortObj = obj => Object.keys(obj).sort().reduce((final, prop) => {
final[prop] = (
// if it's an object, we'll need to sort that...
typeof obj[prop] === 'object'
? (
Array.isArray(obj[prop])
? sortArr(obj[prop])//<-- recursively sort subarray
: sortObj(obj[prop])//<-- recursively sort subobject
)
// otherwise, just retrun the value
: obj[prop]
)
return final
}, {})
// for every element in the given array, deeply sort then stringify it
const deepSortObjectArray = (arr) => arr.map(el => JSON.stringify(sortObj(el)))
// from those strings, create an object with the strings as values and an associated 'true' boolean
const obejctCache = (obj) => deepSortObjectArray(obj).reduce((acc, el) => ({[el]: true, ...acc}), {})
// create an object string cache for every object in the array:
const cacheObjectArrays = arr => arr.map(obj => obejctCache(obj))
// perform an O(1) lookup in each of the caches for a matching value:
const findArrayContainer = (obj, caches) => {
const stringLookupObj = JSON.stringify(sortObj(obj))
return caches.findIndex(cache => cache[stringLookupObj])
}
const array = [
{y: 1, x: 0},
{x: 5, y: 9},
{x: 1, y: 8},
{x: 3, y: {z: 3, x: 1, y: 2}}
]
const arrayArray = [[], [], array]
const cachesArrays = cacheObjectArrays(arrayArray)
console.log(cachesArrays)
/* output: [
{},
{},
{ '{"x":3,"y":{"x":1,"y":2,"z":3}}': true,'{"x":1, "y":8}': true, '{"x":5,"y":9}': true,'{"x":0,"y":1}': true }
]
*/
console.log(findArrayContainer({y: 1, x: 0}, cachesArrays))
// output: 2; working normally!
console.log(findArrayContainer({x: 0, y: 1}, cachesArrays))
// output: 2; working regardless of order!
console.log(findArrayContainer({y: 1, x: 0, q: 0}, cachesArrays))
// output: -1; working as expected with non-found objects!
As you can see, it's pretty complicated. Unless you're 100% this is actually the performance bottleneck, these performance gains may not translate to making interaction smoother.
Let me know if you have any questions about it!

Merge Two Arrays of Objects and combine on same keys

Given A and B of different lengths:
A = [
{X: "a", Y: 5},
{X: "b", Y: 10},
{X: "c", Y: 15}
];
B = [
{X: "a", Z: 5},
{X: "d", Z: 10}
];
Produces the following array:
C = [
{X: "a", Y: 5, Z: 5},
{X: "b", Y: 10},
{X: "c", Y: 15},
{X: "d", Z: 10}
]
So where X is the same the keys that are not X are joined together. Since "a" shares Y and Z it is added together.
Only when X is the same are they joined.
On top of my head a messy solution would be:
C = A.concat(B);
// filter down C to remove duplicate X's
for (i = 0; i < C.length - 1; i++) {
for (j = 1; j < C.length; j++) {
if (C[i]['X'] == C[j]['X']) {
// concatenate the keys together and delete one pair
}
}
}
// keep on looping until no duplicates...
What would be a proper solution for this?
I'm confused about the requirement/question but I believe you want something like the following:
var A = [
{X: "a", Y: 5},
{X: "b", Y: 10},
{X: "c", Y: 15}
];
var B = [
{X: "a", Z: 5},
{X: "d", Z: 10}
];
var C = A.concat(B), temp = {}, result = [];
C.forEach(function(o, i) {
temp[o.X] = temp[o.X] || o;
for(var k in o) temp[o.X][k] = o[k];
});
for(var i in temp) result.push(temp[i]);
console.log(result);
If this is your desired result then it could be re-written in es-6 as well but I kept it simple depending on your code example.

Compare array of objects and return index of object

I've got this array of objects:
[
{x: 615, y: 293, a: 1},
{x: 340, y: 439, a: 0},
{x: 292, y: 505, a: 0}
]
Basically im trying to write a collider. I'd love to return indexes of objects that values of x and y that are equal to each other, how do i approach this?
You can write a function that iterates, via map, through the array and returns the index or a null depending on this condition (x === y) and than filter it, returning only those that are different from null
const collider = array =>
array.map( (item, index) => item.x === item.y ? index : null )
.filter( item => item !== null)
Working fiddle:
https://jsfiddle.net/c3qqmh3b/
var indexes = myArray.reduce((idxs, el, i) => {
if (el.x === el.y) {
return idxs.concat(i);
} else {
return idxs;
}
}, []);
If your myArray would be, for example:
myArray = [
{x: 615, y: 293, a: 1},
{x: 340, y: 340, a: 0},
{x: 292, y: 505, a: 0}
]
then you'll get [1] as a result (because element of index 1 has x===y)
You could just use a hashtable to check for duplicates:
const hash = {};
for(const {x, y} of array) {
if(hash[x + "|" + y])
alert("collides");
hash[x + "|" + y] = true;
}

Best way to get intersection of keys of two objects?

I have two object literals like so:
var firstObject =
{
x: 0,
y: 1,
z: 2,
a: 10,
b: 20,
e: 30
}
var secondObject =
{
x: 0,
y: 1,
z: 2,
a: 10,
c: 20,
d: 30
}
I want to get the intersection of the keys these two object literals have like so:
var intersectionKeys = ['x', 'y', 'z', 'a']
I can obviously do a loop and see if a key with the same name exists in the other object, but I am wondering if this would be a good case for some functional programming and map / filter / reduce usage? I myself have not done that much functional programming, but I have a feeling, that there could exist a clean and clever solution for this problem.
A solution without indexOf.
var firstObject = { x: 0, y: 1, z: 2, a: 10, b: 20, e: 30 },
secondObject = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 };
function intersection(o1, o2) {
return Object.keys(o1).concat(Object.keys(o2)).sort().reduce(function (r, a, i, aa) {
if (i && aa[i - 1] === a) {
r.push(a);
}
return r;
}, []);
}
document.write('<pre>' + JSON.stringify(intersection(firstObject, secondObject), 0, 4) + '</pre>');
Second attempt with O(n).
var firstObject = { x: 0, y: 1, z: 2, a: 10, b: 20, e: 30 },
secondObject = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 };
function intersection(o1, o2) {
return Object.keys(o1).filter({}.hasOwnProperty.bind(o2));
}
document.write('<pre>' + JSON.stringify(intersection(firstObject, secondObject), 0, 4) + '</pre>');
The given answers are nice and astonishing but there could be a problem in void's answer and that is:
"What if one of property values intentionally set to undefined."
Nina's answer is good (really fantastic) but as we are in era of fun JavaScript I think mine wont be too bad:
var a = { x: undefined, y: 1, z: 2, a: 10, b: 20, e: 30 }
var b = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 }
function intersect(o1, o2){
return Object.keys(o1).filter(k => Object.hasOwn(o2, k))
}
document.write('<pre>' + JSON.stringify(intersect(a, b)) + '</pre>');
Update
onalbi mentioned some performance issue in comments which is rational and therefore the code bellow seems to be a better way to handle the problem:
var a = { x: undefined, y: 1, z: 2, a: 10, b: 20, e: 30};
var b = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30};
function intersect(o1, o2) {
const [k1, k2] = [Object.keys(o1), Object.keys(o2)];
const [first, next] = k1.length > k2.length ? [k2, o1] : [k1, o2];
return first.filter(k => k in next);
}
document.write('<pre>' + JSON.stringify(intersect(a, b)) + '</pre>');
The procedure i will suggest is:
Get the array of keys using Object.keys() for one of the objects.
Find the intersection the array using .filter and checking if the second object contains a key matching the first array.
var firstObject = {
x: 0,
y: 1,
z: 2,
a: 10,
b: 20,
e: 30
}
var secondObject = {
x: 0,
y: 1,
z: 2,
a: 10,
c: 20,
d: 30
}
function getIntKeys(obj1, obj2){
var k1 = Object.keys(obj1);
return k1.filter(function(x){
return obj2[x] !== undefined;
});
}
alert(getIntKeys(firstObject, secondObject));
Recursive function
This is other solution, maybe help you. I used a recursive function to intercept two objects. The advantage of this solution is that you not need worry about attributes that are objects at same time.
In this case the function intercept attributes that exist in both objects and asign the value of 'objSource' like final value of attribute intercepeted.
{
function interceptObjects(objSource, objInterface) {
let newObj = {};
for (const key in objSource) {
if (objInterface.hasOwnProperty(key)) {
// in javascript an array is a object too.
if (objSource[key] instanceof Object && !Array.isArray(objSource[key]) && objInterface[key] instanceof Object && !Array.isArray(objInterface[key])) {
newObj[key] = {};
newObj[key] = interceptObjects(objSource[key], objInterface[key])
} else {
newObj[key] = objSource[key];
}
}
}
return newObj;
}
// FOR TESTING
let objSource = {
attr1: '',
attr2: 2,
attr3: [],
attr4: {
attr41: 'lol',
attr42: 12,
attr43: 15,
attr45: [1, 4],
},
attr5: [2, 3, 4],
};
let objInterface = {
attr1: null,
attr4: {
attr41: null,
attr42: 12,
attr45: [1],
},
attr5: [],
attr6: null,
};
console.log(this.interceptObjects(objSource, objInterface));
}
Here is a simple entry, very functional, handles any number of objects, and returns the values of the matching keys from the first object passed.
This behavior is similar to that of array_intersect_key() in PHP in case anyone is searching for that.
function intersectKeys(first, ...rest) {
const restKeys = rest.map(o => Object.keys(o));
return Object.fromEntries(Object.entries(first).filter(entry => restKeys.every(rk => rk.includes(entry[0]))));
}
Expanded here for better explanation and commenting
function intersectKeys(first, ...rest) {
// extract the keys of the other objects first so that won't be done again for each check
const restKeys = rest.map(o => Object.keys(o));
// In my version I am returning the first objects values under the intersect keys
return Object.fromEntries(
// extract [key, value] sets for each key and filter them, Object.fromEntries() reverses this back into an object of the remaining fields after the filter
Object.entries(first).filter(
// make sure each of the other object key sets includes the current key, or filter it out
entry => restKeys.every(
rk => rk.includes(entry[0])
)
)
);
// to get JUST the keys as OP requested the second line would simplify down to this
return Object.keys(first).filter(key => restKeys.every(rk => rk.includes(key)));
}
It's important to note that this solution only works on string keys, Symbol keys will be ignored and the final object will not contain any. Though a similar function could be written to compare Symbol intersect as well.
I know this is an old post, however, I want to share a solution I wrote today that I believe is efficient and clean.
function intersectingKeys(...objects) {
return objects
.map((object) => Object.keys(object))
.sort((a, b) => a.length - b.length)
.reduce((a, b) => a.filter((key) => b.includes(key)));
}
This function can take in n number of objects, and find the intersecting keys.
This is how it works.
Map the objects, creating an array of key arrays.
Sort the array by length, this puts the smallest key arrays first.
Finally, reduce our key arrays, by filtering each list of keys against the next list.
I think the clever part of this algorithm is the pre sorting of the key arrays. By starting with the smallest list of keys, we have less work to do comparing keys.
Here is the usuage:
var firstObject = {
x: 0,
y: 1,
z: 2,
a: 10,
b: 20,
e: 30,
};
var secondObject = {
x: 0,
y: 1,
z: 2,
a: 10,
c: 20,
d: 30,
};
intersectingKeys(firstObject, secondObject);
// [ 'x', 'y', 'z', 'a' ]

Query array of objects in JavaScript

I have an array of coordinates like this:
coordinates = [
{x: 1, y: 2},
{x: 3, y: 4},
{x: 5, y: 6},
{x: 7, y: 8},
{x: 9, y: 0}
];
I want to query this array for an object like this.
var searchFor = {x: 1, y: 2}
I tried this:
if ($.inArray(searchFor, coordinates) !== -1) {
...
}
But this always return -1. All I need is true/false info about whether the object is in this array. How can I achieve this?
This is because objects are not equal to each other - even if they have the same properties/values - unless they are the exact same instance.
What you would have to do is manually iterate through the array:
for( var i=0, l=coordinates.length, found = false; i<l; i++) {
if( coordinates[i].x == searchFor.x && coordinates[i].y == searchFor.y) {
found = true;
break;
}
}
if( found) {
// ...
}
If you want a convenient one-liner solution, you could work with Lo-Dash.
_(coordinates).findIndex({x: 3, y: 4})
// 1
Here's a more generic approach for searching for an object within the array of objects:
Array.prototype.indexOfObj = function(o,exact){
// make sure incoming parameter is infact an object
if (typeof o === 'object'){
// iterate over the elements of the origin array
for (var i = 0; i < this.length; i++){
var match = true,
to = this[i],
matchedKeys = [];
// search through o's keys and make sure they exist and
// match the keys in the origin array
for (var k in o){
match &= o.hasOwnProperty(k) && to.hasOwnProperty(k);
if (match){
matchedKeys.push(k);
match &= (k in to && to[k] == o[k]);
}
}
// if we need an exact match, map it backwards as well
// (all of o's keys == all of to's keys)
if (match && exact){
for (var k in to){
match &= to.hasOwnProperty(k);
// additional unmatched keys
if (match && matchedKeys.indexOf(k) == -1){
match = false;
break;
}
}
}
// if it was a match, return the current key
if (match){
return i;
}
}
}
// default to to match found result
return -1;
}
Then, using your example:
{x:98,y:99} non-exact = -1
{x:98,y:99} exact = -1
{x:1} non-exact = 0
{x:1} exact = -1
{x:5,y:6} non-exact = 2
{x:5,y:6} exact = 2
use taffy DB, Taffy DB
var coordinates = [ {x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 6}, {x: 7, y: 8}, {x: 9, y: 0}];
var coordinatesDB = TAFFY(coordinates);
res = coordinatesDB({x: 1, y: 2});
You could use $.grep - http://api.jquery.com/jQuery.grep/
coordinates = [{x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 6}, {x: 7, y: 8}, {x: 9, y: 0}];
var query = $.grep(coordinates, function(co){ return co.x == 1 && co.y == 2; });
var hasResult = (query.length !== 0)
// query = {x: 1, y:2} - hasResult = true
As mentioned by others, you can not compare two unique objects contents by comparing the objects themselves, so you have to compare their properties. You could do something like this with Array.prototype.some which is ECMA5 but can easily be shimmed.
Javascript
function indexOfCoordinates(array, object) {
var index = -1;
array.some(function (coordinate, arrayIndex) {
if (coordinate.x === object.x && coordinate.y === object.y) {
index = arrayIndex;
return true;
}
return false;
});
return index;
}
var coordinates = [
{x: 1, y: 2},
{x: 3, y: 4},
{x: 5, y: 6},
{x: 7, y: 8},
{x: 9, y: 0}
];
if (indexOfCoordinates(coordinates, {x: 5, y: 6}) !== -1) {
console.log("found");
}
if (indexOfCoordinates(coordinates, {x: 9, y: 1}) === -1) {
console.log("not found");
}
On jsfiddle
Or as you suggested, you only want true or false then you can further simplify.
Javascript
function hasCoordinate(array, object) {
return array.some(function (coordinate) {
return coordinate.x === object.x && coordinate.y === object.y;
});
}
var coordinates = [
{x: 1, y: 2},
{x: 3, y: 4},
{x: 5, y: 6},
{x: 7, y: 8},
{x: 9, y: 0}
];
if (hasCoordinate(coordinates, {x: 1, y: 2})) {
console.log("found");
}
if (!hasCoordinate(coordinates, {x: 9, y: 1})) {
console.log("not found");
}
On jsfiddle
This could be further generalised using ECMA5 methods Object.keys and Array.prototype.map, should you for example, change the references x and y to a and b, or extend your coordinates to include z. Now your function would still work without need of alteration.
Javascript
function hasCoordinate(array, object) {
var objectKeys = Object.keys(object).sort(),
objectValues = objectKeys.map(function (value) {
return object[value];
});
return array.some(function (coordinate) {
var coordinateKeys = Object.keys(coordinate).sort(),
coordinateValues = coordinateKeys.map(function (value) {
return coordinate[value];
});
return coordinateKeys.toString() === objectKeys.toString() && coordinateValues.toString() === objectValues.toString();
});
}
var coordinates = [
{x: 1, y: 2},
{x: 3, y: 4},
{x: 5, y: 6},
{x: 7, y: 8},
{x: 9, y: 0}
];
if (hasCoordinate(coordinates, {x: 1, y: 2})) {
console.log("found");
}
if (!hasCoordinate(coordinates, {x: 9, y: 1})) {
console.log("not found");
}
On jsfiddle
Of course you could continue further along the generic route, and even introduce recursion.

Categories

Resources