Getting index of array object element given multiple object keys - javascript

I know given a single key (for example, if I know the object.name = 'Sam') using:
var index = array.map(function(el) {return el.name}).indexOf('Sam');
I can get the index of the array element with object.name = 'Sam'
However say I have several elements with object.name ='Sam' in the array, but now I know know the object.name, object.age and object.size - is it possible to adapt the above code to get the index but also checking against object.age and object.size?

Assuming you have the values in variables such as name, age and size as you mentioned in comments, You can use a function like:
function findInArray(arr) {
for (var i = 0; i < arr.length; i++) {
var el = arr[i];
if (el.name == name && el.age == age && el.size == size)
return i;
}
return -1;
};
Which will return the index of object in array if match is found, and -1 otherwise...
var data = [{
name: "Sis",
age: "17",
size: "10"
}, {
name: "Sam",
age: "17",
size: "10"
}, {
name: "Som",
age: "17",
size: "10"
}],
name = "Sam",
age = "17",
size = "10";
function findInArray(arr) {
for (var i = 0; i < arr.length; i++) {
var el = arr[i];
if (el.name == name && el.age == age && el.size == size)
return i;
}
return -1;
};
console.log(findInArray(data));

If you're using the awesome underscore library there's a _.findWhere function.
var sam21 = _.findWhere(people, {
name: 'Sam',
age: 21
});
if you want something without a whole other library you can use .filter.
var sam21 = people.filter(function(person) {
return person.age === 21 && person.name === 'Sam';
});
I just noticed you're looking for the index. This answer can be useful: https://stackoverflow.com/a/12356923/191226

You could use a function like this one
indexOfObjectArray = function(array, keyvalues) {
for (var i = 0; i < array.length; i++) {
var trueCount = 0;
for (var j = 0; j < keyvalues.length; j++) {
if (array[i][keyvalues[j]['key']] == keyvalues[j]['value']) {
trueCount++;
}
}
if (trueCount === keyvalues.length) return i;
}
return -1; }
and use it like that for example:
var yourArray = [{id: 10, group: 20},...];
var keyvalues = [
{ 'key': 'id', 'value': 10 },
{ 'key': 'group', 'value': 20 }];
var index = indexOfObjectArray(yourArray, keyvalues );
This function will return the index of an object that has id = 10 and group = 20

Related

Javascript: object array mapping and matching with IE11

I'm looking for a javascript implementation (for IE11) for this problem; my inputs are two arrays like these:
var array1 = [{id: 1, param:"bon jour"}, {id: 2, param:"Hi"}, {id: 3, param:"Hello"}];
var array2 = [{item: "Peter", values:"1,2", singlevalue:"2"},
{item: "Mark", values:"1,2,3", singlevalue:"3"},
{item: "Lou", values:"2", singlevalue:"2"}];
and I should create a new array (array3) with array2 data plus 2 new fields ("params" and "singleparam"), using matching between array1[i].id and array2[x].values to evaluate "params" and between array1[i].id and array2[x].singlevalue to evaluate "singleparam", with this kind of result:
array3 = [{item: "Peter", values:"1,2", singlevalue:"2", params:"bon jour,Hi", singleparam:"Hi"},
{item: "Mark", values:"1,2,3", singlevalue:"3", params:"bon jour,Hi,Hello", singleparam:"Hello"},
{item: "Lou", values:"2", singlevalue:"2", params:"Hi", singleparam:"Hi"}];
I'm a javascript newbie and I've tried this kind of solution:
var array3 = array2.map(function(x, array1)
{
const newOb = {};
newOb.item = x.item;
newOb.values = x.values;
newOb.singlevalue = x.singlevalue;
newOb.params = function(x.values, array1)
{
var str = "";
var idArray = x.values.split(",");
for(i = 0; i < idArray.lenght; i++)
{
for(j = 0; i < array1.lenght; j++)
{
if(idArray[i] == array1[j].id)
{
str += array1[j].param + ",";
break;
}
}
}
return str;
};
newOb.singleparam = function(x.singlevalue, array1)
{
var val;
for(j = 0; i < array1.lenght; j++)
{
if(array1[j].id == x.singlevalue)
val = array1[j].param;
}
return val;
}
return newOb;
});
console.log(array3);
with this error: Error: Unexpected token '.'
I'd like to find an efficient solution considering that array1 has less than 10 elements, but array2 could contains more than 1000 objects.
Thanks in advance for your support
I will skip the functions stop and singlevalues and there were also some syntax errors,
for example the correct one is length and not lenght
var array1 = [{id: 1, param:"bon jour"}, {id: 2, param:"Hi"}, {id: 3, param:"Hello"}];
var array2 = [{item: "Peter", values:"1,2", singlevalue:"2"},
{item: "Mark", values:"1,2,3", singlevalue:"3"},
{item: "Lou", values:"2", singlevalue:"2"}];
function newArray3() {
return array2.map(x => {
const newOb = {};
newOb.item = x.item;
newOb.values = x.values;
newOb.singlevalue = x.singlevalue;
newOb.params = paramsFunction(x.values, array1);
newOb.singleparam = singleParamFunction(x.singlevalue, array1);
return newOb;
})
}
function singleParamFunction(x, array1) {
var val;
for(i = 0; i < array1.length; i++) {
if(array1[i].id.toString() == x) {
val = array1[i].param;
}
}
return val;
}
function paramsFunction(x, array1) {
var str = "";
var idArray = x.split(",");
for(i = 0; i < idArray.length; i++)
{
for(j = 0; j < array1.length; j++)
{
if(idArray[i] == array1[j].id.toString())
{
str += array1[j].param + ",";
break;
}
}
}
return str;
}
array3 = newArray3();
console.log(array3)
The solution provided by the #Walteann Costa can show the desired results in other browsers but it will not work for the IE browser as his code sample uses the => Arrow functions that is not supported in the IE browser.
As your question asks the solution for the IE browser, I tried to modify the code sample provided by the #Walteann Costa. Below modified code can work with the IE 11 browser.
<!doctype html>
<html>
<head>
<script>
"use strict";
var array1 = [{
id: 1,
param: "bon jour"
}, {
id: 2,
param: "Hi"
}, {
id: 3,
param: "Hello"
}];
var array2 = [{
item: "Peter",
values: "1,2",
singlevalue: "2"
}, {
item: "Mark",
values: "1,2,3",
singlevalue: "3"
}, {
item: "Lou",
values: "2",
singlevalue: "2"
}];
function newArray3() {
return array2.map(function (x) {
var newOb = {};
newOb.item = x.item;
newOb.values = x.values;
newOb.singlevalue = x.singlevalue;
newOb.params = paramsFunction(x.values, array1);
newOb.singleparam = singleParamFunction(x.singlevalue, array1);
return newOb;
});
}
function singleParamFunction(x, array1) {
var val,i,j;
for (i = 0; i < array1.length; i++) {
if (array1[i].id.toString() == x) {
val = array1[i].param;
}
}
return val;
}
function paramsFunction(x, array1) {
var str = "";
var idArray = x.split(",");
var i,j;
for (i = 0; i < idArray.length; i++) {
for (j = 0; j < array1.length; j++) {
if (idArray[i] == array1[j].id.toString()) {
str += array1[j].param + ",";
break;
}
}
}
return str;
}
var array3 = newArray3();
console.log(array3[0]);
console.log(array3[1]);
console.log(array3[2]);
</script>
</head>
<body>
</body>
</html>
Output in the IE 11:

JavaScript -- find matching object in array of objects

I am trying to search for an object in an array of objects.
Note, vals and recs objects will be DYNAMIC.
var vals = {ID: "4", LOC: "LA", SEQ: "1"};
var recs = [
{ID:"4", LOC:"LA", SEQ:"1"},
{ID:"4", LOC:"NY", SEQ:"1"},
{ID:"4", LOC:"CHI",SEQ:"1"}
];
Now I need to check if all key:value pairs in vals already exist in recs . In this case, recs[0] is an exact match of vals.
Heres my attempt:
var vals = {ID: "4", LOC: "LA", SEQ: "1"};
var recs = [
{ID:"4", LOC:"LA", SEQ:"1"},
{ID:"3", LOC:"NY", SEQ:"2"},
{ID:"2", LOC:"CHI",SEQ:"3"}
];
for(var i = 0; i<recs.length; i++){
if(recs[i]["ID"] == vals["ID"] && recs[i]["LOC"] == vals["LOC"] && recs[i]["SEQ"] == vals["SEQ"]){
console.log(true);
}
else{
console.log(false);
}
}
The above works only because I have hardcoded the keys from the vals object. In reality, the VALS object (and recs) will be DYNAMIC with X number of key:value pairs.
So how can I modify my for loop for a dynamic vals object?
thanks!
Try this:
for (var i = 0; i < recs.length; i++) {
var found = true;
for (var p in vals) {
if (vals.hasOwnProperty(p)) {
if (recs[i][p] !== vals[p]) {
found = false;
break;
}
}
}
console.log(found);
}
for(var i = 0; i<recs.length; i++) {
for (var prop in object) {
if (recs[i][prop] != vals[prop]) {
console.log(false);
return;
}
}
//check from both sides
for (var prop in vals) {
if (recs[i][prop] != vals[prop]) {
console.log(false);
return;
}
}
console.log(true);
}
You could iterate over the keys; something along the lines of:
var vals = { ID: "4", LOC: "LA", SEQ: "1", REGION: "USA" };
var recs = [{ ID: 4, LOC: "LA", SEQ: "1", REGION: "USA" },
{ ID: 3, LOC: "NY", SEQ: "2", REGION: "USA" },
{ ID: 2, LOC: "CHI", SEQ: "3", REGION: "USA" }
];
var isSame = true;
for (var i = 0; i < recs.length; i++) {
console.log( i + '----------------' );
var isSame = true;
// get the keys of the record
var keys = Object.keys(recs[i]);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var record = recs[i]
console.log( key + ": " + record[key] + '=' + vals[key] );
if (record[key] != vals[key] ) {
isSame = false;// not equal
break;
}
}
console.log('isSame: ' + isSame );
console.log('------------------' );
}
You need to break it into two loops, one for each object of the array and one for each key of the object:
for(var i = 0; i<recs.length; i++){
var found = false
for(var key in recs[i]) {
if(recs[i].hasOwnProperty(key)){
if(recs[i][key] != vals[key]){
found = true
}
}
console.log(found)
}
the hasOwnProperty call will make sure it doesn't break if the object does not have that key.
You can try this:
function myFind(recs, vals) {
return recs.some(function(obj) {
for (var x in obj)
if (x in vals && obj[x] != vals[x])
return false;
return true;
});
}
var recs = [
{ID:4, LOC:"LA", SEQ:"1", USA:"USA"},
{ID:3, LOC:"NY", SEQ:"2", USA:"USA"},
{ID:2, LOC:"CHI",SEQ:"3", USA:"USA"}
];
var vals = {ID: "4", LOC: "LA", SEQ: "1"};
if (myFind(recs, vals)) {
alert('found');
} else {
alert('not found');
}
Hope it helps.
you can use underscorejs isEqual for this kind of problem

How to check if array is unique on specific object property?

I have an array of objects:
var array1 = [
{
property1: 10,
property2: "abc"
},
{
property1: 11,
property2: "def"
},
{
property1: 10,
property2: "ghi"
}
];
Now what I want is this array will be said not unique as per value of property1.
This means that this array contains 2 elements with property1=10, so the array does not contain unique value of property1.
To check this, I can use a for loop:
for (var i = 0; i < array1.length; i++) {
var array2 = array1.slice(); // copy array
array2.remove(array1[i]);
var temppropety1 = array1[i].property1;
for (var j = 0; j < array2.length; j++) {
if (array2[J].property1==temppropety1) {
return true;
}
}
}
But is there an easier way or a library to find this?
Here is a straightforward way to test for uniqueness on property1. Loop through the objects in the outer array and add each object's property1 to a temp array if it is not already in that temp array. If a duplicate value is encountered, return false meaning property1 is not unique.
function isUnique(arr) {
var tmpArr = [];
for(var obj in arr) {
if(tmpArr.indexOf(arr[obj].property1) < 0){
tmpArr.push(arr[obj].property1);
} else {
return false; // Duplicate value for property1 found
}
}
return true; // No duplicate values found for property1
}
Demo: http://jsbin.com/lohiqihipe/1/
First, you could reduce (aggregate) the objects by grouping them by the value of property1:
var grouped = array.reduce(function(grouped, item) {
var propertyValue = item.property1;
grouped[propertyValue] = (grouped[propertyValue] || 0) + 1;
return grouped;
}, {});
Then you check that every key of the resulting object has a value of 1:
var result = Object.keys(grouped).every(function(key) {
return grouped[key] === 1;
});
I suggest that array can be quite big so I'd prefer not to copy it and just validate properties.
Also it is not an option to use map function of array because in this case you won't be able to break a cycle on first match:
var equals = function(array) {
var co = {};
var unique = true;
for(var i = 0; i < array.length; i++) {
var o = array[i];
if (co[o.property1]) {
unique = false;
break;
} else {
co[o.property1] = true;
}
}
return unique;
};
You can convert your array to flat structure:
array1.map(function(item) { return item.property1; });
and now your problem simplify to check duplicates in simple array
var array1 = ["a","b","b","c","d","e","f"];
var uniqueItems = [];
$.each(array1, function(i, el){
if($.inArray(el, uniqueItems) === -1) uniqueItems.push(el);
});
References:
https://stackoverflow.com/a/840808/4772988
https://stackoverflow.com/a/9229932/4772988
You can use a couple of helpers to abstract it:
var uniqBy = function(f, xs) {
var seen = []
return xs.filter(function(x) {
var fx = f(x)
if (seen.indexOf(fx) > -1) return
seen.push(fx)
return true
})
}
var dot = function(k) {
return function(obj) {
return obj[k]
}
}
Then filter out duplicates by the property, and compare the length of the result to the original array. If they don't match, then they must not be unique:
var res = uniqBy(dot('property1'), array1)
var isUnique = array1.length === res.length
console.log(isUnique) // false
If you got only numbers or only strings to remove duplicates from, then you can improve performance by using an object instead of an array to keep track of elements seen so far.
You can use lodash library to achieve this.
Here is the library documentation: https://lodash.com/docs/4.17.5#filter
Method:-
function isDuplicatesPresent(list, propertyName){
return _.filter(list, function (value) {
return _.filter(list, function(innerValue){ reutrn innerValue[propertyName] === value[propertyName]}).length > 1;
}).length > 0;
}
Example:-
var users = [
{ user: 'barney', age: 36, active: true },
{ user: 'fred', age: 40, active: false },
{ user: 'barney', age: 37, active: true}
];
let duplicates = _.filter(users, function (value) {
return _.filter(users, {user:value.user}).length > 1;
});
Result:
console.log(duplicates)
> [
{"user": "barney","age": 36,"active": true},
{"user": "barney","age": 37,"active": true}
];

Remove JSON entry by value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Delete from array in javascript
I have the following JSON object:
[id:84,id:92,id:123,id:2353]
How would I go about removing the item which the value is "123" using javascript?
or if I formatted the json as
[84, 92, 123, 2353]
How would it be removed in this case?
Assume you have this:
var items = [{ id: 84 }, { id: 92 }, { id: 123 }, { id: 2353 }];
var filtered = items.filter(function(item) {
return item.id !== 123;
});
//filtered => [{ id: 84 }, { id: 92 }, { id: 2353 }]
Supposing you actually have an object from a json in the json variable
for (key in json) {
if (json.hasOwnProperty(key) && json[key] == 123) {
delete json[key];
}
}
Shorter alternative would be:
var newArr = [{id:84}, {id:92}, {id:123}, {id:2353}].filter(function(a) {
return a.id != 123;
});
If you have this:
var arr = [{id:84}, {id:92}, {id:123}, {id:2353}]
To remove the item with value 123, you can do:
for(var i = 0; i < arr.length; i++) {
if(arr[i].id == 123) {
arr.splice(i, 1);
break;
}
}
function removeClass(obj, cls) {
var classes = obj.className.split(' ');
for(i=0; i<classes.length; i++) {
if (classes[i] == cls) {
classes.splice(i, 1);
i--; // (*)
}
}
obj.className = classes.join(' ');
}
var obj = { className: 'open menu menu' }
removeClass(obj, 'menu')
alert(obj.className)
You can use splice function, like this:
var data = [{id:84}, {id:92}, {id:123}, {id:2353}];
function remove(){
for(var i = 0, max = data.length; i < max; i++) {
var a = data[i];
if(a.id === 123) {
data.splice(i, 1);
break;
}
}
}
remove();
Seems like you want to avoid a loop. Assuming it's available, you can use .filter:
[{id:84},{id:92},{id:123},{id:2353}]
.filter(function (elem) { return elem.id !== 123; });
This technically does do a loop, but at least you don't have to look at it.
Assuming your "json" is really an array, like [84, 92, 123, 2353]:
var myString = "[84, 92, 123, 2353]";
var myArray = JSON.parse(myString);
var index = myArray.indexOf(123); // whatever value you are looking for
myArray.splice(index, 1);
http://jsfiddle.net/7vkK6/
Assuming I'm understanding your question and comments correctly you can do something like this:
var old_array = [{id: 84},...];
var new_array = [];
for(var i = 0, len = old_array.length; i++) {
if (old_array[i].id != 123) new_array.push(old_array[i]);
}
What you have currently is not JSON so I'll give you some different options.
If you have an Array arr = [84,92,123,2353] then
arr = arr.filter(function (x) {return x !== 123;}); // all occurrences
// OR
arr.splice(arr.indexOf(123), 1); // first occurrence only
If you have an Object obj = {"84": a, "92": b, "123": c, "2353": d}, a to d some expressions, then
delete obj['123']; // obj now {"84": a, "92": b, "2353": d}
1) JSON is a string, not an array or an object.
var json = "[1,2,3]";
2) Valid JSON NEEDS to be valid JS
var myJSObj = { 1,2,3 }, // broken
myJSArr = [ name : 1, name2 : 2 ]; // broken
3) If you have a JS Array, you can remove an element by using [].splice
var arr = [ 1, 2, 3, 4 ],
i = 0, l = arr.length,
test = 4;
for (; i < l; i += 1) {
if (arr[i] === test) { arr.splice(i, 1); } // remove 1 starting at i
}
4) If you have an object with named keys, you can use delete
var obj = { val : 1 };
delete obj.val;

Need an algorithm to manipulate array structure in javascript

In javascript, here is my start array:
[{
name: 'aaa',
value: 1
},
{
name: 'bbb',
value: 0
},
{
name: 'bbb',
value: 1
}]
I want to transform it into this array as result:
[{
name: 'aaa',
value: 1
},
{
name: 'bbb',
value: [0, 1]
}]
I need a good and simple algorithm to do this
How about:
var array = [{
name: 'aaa',
value: 1
},
{
name: 'bbb',
value: 0
},
{
name: 'bbb',
value: 1
}];
var map = {};
for(var i = 0; i < array.length; i++) {
var name = array[i].name;
if (map[name] === undefined) {
map[name] = [];
}
map[name].push(array[i].value);
}
var result = [];
for(var key in map) {
var value = map[key];
result.push({
name: key,
value: value.length === 1 ? value[0] : value
});
}
Easiest way is to create a map to keep track of which names are used. Then convert this map back to an array of objects.
If you want to use Arrays for value then change it to:
result.push({
name: key,
value: value
});
here's pseudocode for simplest implementation
hash = {}
for(pair in array) {
hash[pair.name] ||= []
hash[pair.name] << pair.value
}
result = []
for(k, v in hash) {
result << {name: k, value: v}
}
This function does the trick
function consolidate(var arrayOfObjects)
{
// create a dictionary of values first
var dict = {};
for(var i = 0; i < arrayOfObjects.length; i++)
{
var n = arrayOfObjects[i].name;
if (!dict[n])
{
dict[n] = [];
}
dict[n].push(arrayOfObjects[i].value);
}
// convert dictionary to array again
var result = [];
for(var key in dict)
{
result.push({
name: key,
value: dict[key].length == 1 ? dict[key][0] : dict[key]
});
}
return result;
}
An alternative solution:
function convert(arr) {
var res = [];
var map = {};
for (var i=0;i<arr.length;i++) {
var arrObj = arr[i];
var oldObj = map[arrObj.name];
if (oldObj == undefined) {
oldObj = {name:arrObj.name, value:arrObj.value};
map[arrObj.name] = oldObj;
res.push(oldObj);
} else {
if( typeof oldObj.value === 'number' ) {
oldObj.value = [oldObj.value];
}
oldObj.value.push(arrObj.value);
}
}
return res;
}
In theory it should work a bit faster and use less memory. Basically it creates a result array and a map which is an index for the same array (no duplicate objects). So it fills the result in one iteration instead of two and does not need to convert map to array (which saves several CPU cycles :P ).
Added:
Here is a variation of that function in case value: [1] is acceptable:
function convert(arr) {
var res = [];
var map = {};
for (var i=0;i<arr.length;i++) {
var arrObj = arr[i];
var oldObj = map[arrObj.name];
if (oldObj == undefined) {
oldObj = {name:arrObj.name, value:[arrObj.value]};
map[arrObj.name] = oldObj;
res.push(oldObj);
} else {
oldObj.value.push(arrObj.value);
}
}
return res;
}

Categories

Resources