json index of property value - javascript

I need to get the index of the json object in an array whose by the objects id
here is the example code
var list = [ { _id: '4dd822c5e8a6c42aa70000ad',
metadata:
{ album: 'American IV: Man Comes Around',
song: 'Hurt',
coverart: 'http://images.mndigital.com/albums/044/585/435/m.jpeg',
artist: 'Johnny Cash',
length: 216,
mnid: '44585439' } },
{ _id: '4dd80b16e8a6c428a900007d',
metadata:
{ album: 'OK Computer (Collector\'s Edition)',
song: 'Paranoid Android',
coverart: 'http://images.mndigital.com/albums/026/832/735/m.jpeg',
artist: 'Radiohead',
length: 383,
mnid: '26832739' } },
{ _id: '4dd68694e8a6c42c80000479',
metadata:
{ album: 'The Presidents Of The United States Of America: Ten Year Super Bonus Special Anniversary Edition',
song: 'Lump',
coverart: 'http://images.mndigital.com/albums/011/698/433/m.jpeg',
artist: 'The Presidents Of The United States Of America',
length: 134,
mnid: '11698479' } }
]
then pseudo code
var index_of = list.indexOf("4dd80b16e8a6c428a900007d");
obviously that is not gonna work but I wonder if there is anyway to do this without looping to find the index ?

var i = list.length;
while( i-- ) {
if( list[i]._id === '4dd80b16e8a6c428a900007d' ) break;
}
alert(i); // will be -1 if no match was found

You can try Array.prototype.map, based on your example it will be:
var index = list.map(function(e) { return e._id; }).indexOf('4dd822c5e8a6c42aa70000ad');
Array.prototype.map is not available on IE7 or IE8. ES5 Compatibility

Since there is no index of your data structure by _id, something is going to have to loop over the structure and find an _id value that matches.
function findId(data, id) {
for (var i = 0; i < data.length; i++) {
if (data[i]._id == id) {
return(data[i]);
}
}
}

Just loop through the list until you find the matching id.
function indexOf(list, id) {
for (var i = 0; i < list.length; i++) {
if (list[i]._id === id) { return i; }
}
return -1;
}

function index_of(haystack, needle) {
for (var i = 0, l = haystack.length; i < l; ++i) {
if( haystack[i]._id === needle ) {
return i;
}
}
return -1;
}
console.log(index_of(list, '4dd80b16e8a6c428a900007d'));
same as the above, but caches the length of the haystack.

a prototypical way
(function(){
if (!Array.prototype.indexOfPropertyValue){
Array.prototype.indexOfPropertyValue = function(prop,value){
for (var index = 0; index < this.length; index++){
if (prop in this[index]){
if (this[index][prop] == value){
return index;
}
}
}
return -1;
}
}
})();
// usage:
list.indexOfPropertyValue('_id','4dd80b16e8a6c428a900007d'); // returns 1 (index of array);
list.indexOfPropertyValue('_id','Invalid id') // returns -1 (no result);
var indexOfArray = list.indexOfPropertyValue('_id','4dd80b16e8a6c428a900007d');
list[indexOfArray] // returns desired object.

Related

Compare two arrays with objects and show which item is deleted

I have two arrays which I want to compare and check if there is an deleted item in one of these arrays. If there is show me the difference (deleted item)
Here is the code below how I would like to achieve this:
function compareFilters (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
console.log('k', k);
diff.push(k);
}
return diff;
}
console.log(filters);
console.log(filters, queryBuilderFilters, compareFilters(queryBuilderFilters, filters));
This will log both arrays which look like this:
[
0: {
id: "AggregatedFields.ReturnOnAdSpend",
label: "ROAS (Return on Ad Spend)",
type: "integer",
description: "The ROAS (Return on Ad Spend)."
},
1: {
id: "AggregatedFields.ROIExcludingAssisted",
label: "ROI excl. assisted",
type: "integer",
description: "The ROI excluding any assisted values.
}
]
And the output of the compareFilters function is 0: "[object Object]"
How can I return the label of the object in this function?
This example illustrates what you want
var completedList = [1,2,3,4,7,8];
var invalidList = new Set([3,4,5,6]);
// filter the items from the invalid list, out of the complete list
var validList = completedList.filter((item) => {
return !invalidList.has(item);
})
console.log(validList); // Print [1,2,7,8]
// get a Set of the distinct, valid items
var validItems = new Set(validList);
You can try this, supposing that the objects in each array have the same reference and are not copies:
function compareFilters (a1, a2) {
const a1l = a1.length;
const a2l = a2.length;
// Both arrays are considered to be equal
if(a1l === a2l) return;
let completed;
let unCompleted;
let deletedValue;
if(a1l > a2l) {
completed = a1;
unCompleted = a2;
} else {
completed = a2;
unCompleted = a1;
}
for(let i = 0; i < completed.lenth; i++) {
if(completed[i] !== unCompleted[i]) {
return completed[i].label;
}
}
}
It will return undefined in case both arrays has the same quantity of elements. Otherwise, it will return the label of first element that is in one array but not the another.

Decrement ID in JSON array after deletion

I have an array stored in my local storage. It is dynamic. I'm storing the data along with an ID which gets incremented after every entry. User has an option of deleting, hence, I'm supposed to remove the corresponding element from the array. But, I want the ID to remain in ascending order. Ex:
var jsonObj = [{'id':'1','name':'Ray','email':'ray#gmail.com'},
{'id':'2','name':'Steve','email':'steve#gmail.com'},
{'id':'3','name':'Albert','email':'albert#gmail.com'},
{'id':'4','name':'Christopher','email':'chris#gmail.com'}]
I'm creating HTML divs for the above array. In case, Steve deletes his details, I want the array to be like:
var jsonObj = [{"id":1,"name":"Ray","email":"ray#gmail.com"},
{"id":2,"name":"Albert","email":'albert#gmail.com"},
{"id":3,"name":"Christopher","email":"chris#gmail.com"}]
The following code doesn't work accordingly.
for (var i=0; i<jsonObj.length; i++) {
//delete function is working fine.
jsonObj[i].id--;
break;
}
You could just iterate from the given index and decrement the id property.
function deleteItem(i) {
array.splice(i, 1);
while (i < array.length) {
array[i].id--;
i++;
}
}
var array = [{ id: '1', name: 'Ray', email :'ray#gmail.com'}, { id: '2', name: 'Steve', email: 'steve#gmail.com' }, { id: '3', name: 'Albert', email: 'albert#gmail.com' }, { id: '4', name: 'Christopher', email: 'chris#gmail.com' }];
deleteItem(1);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you start from 0, then you do not even need the ID
Ray is the 0th element, Christopher is 3rd
delete Albert and Christopher is 2nd
var jsObj = [{'name':'Ray','email':'ray#gmail.com'},
{'name':'Steve','email':'steve#gmail.com'},
{'name':'Albert','email':'albert#gmail.com'},
{'name':'Christopher','email':'chris#gmail.com'}]
for (var i=0;i<jsObj.length;i++) {
document.write("<br>ID"+(i+1)+": "+jsObj[i].name)
}
document.write("<hr/>");
jsObj.splice(2, 1); // Bye bye Albert
for (var i=0;i<jsObj.length;i++) {
document.write("<br>ID"+(i+1)+": "+jsObj[i].name)
}
More information
Reindex javascript array / object after removing a key
You just declare a new variable in your for loop which you will increment it and you assign this value as their id
for (var i=0, id=1; i<jsonObj.length; i++, id++) {
var jsonObj = [{'id':'1','name':'Ray','email':'ray#gmail.com'},
{'id':'2','name':'Steve','email':'steve#gmail.com'},
{'id':'3','name':'Albert','email':'albert#gmail.com'},
{'id':'4','name':'Christopher','email':'chris#gmail.com'}];
console.log(jsonObj);
jsonObj.splice(1, 1);
for (var i=0, id=1; i<jsonObj.length; i++, id++) {
jsonObj[i].id = id;
}
console.log(jsonObj);
a very simplistic approach could be:
// the index of the deleted element
const delIndex = 2;
// reindex all entries starting from deleted one
for (var i=delIndex+1; i<jsonObj.length; i++) {
jsonObj[i].id = i + 1;
}
The id basically corresponds with the array index anyway. So instead of trying to compute the id anew, we can just overwrite it with the respective index (+1 as you start with one and not zero like array indices).
for your requirements you have to use the splice method in the javascript
array.splice(index_you_wantto_delete,count)
ex:-jsonObj.splice(1,1);
The splice() method adds/removes items to/from an array,
Here is a verbose solution explaining the process step by step
1- Delete the element
2 - Update the indexes if the element was found and deleted
/**
*
* #param array list Your list
* #param int elementID The id of the element you want to remove
* #returns list The list with the element removed and the indexes rearanged
*/
var deleteElement = function (list, elementID) {
var removedIndex = false;
for (var index = 0; index < list.length; index++) {
if (list[index]['id'] === elementID) {
list.slice(index, 1);
removedIndex = index;
break;
}
}
if (removedIndex !== false) {
//Get last id
var lastElement = (removedIndex === 0) ? null : list[removedIndex - 1];
// Set to 0 if the first element was removed
var lastID = (lastElement === null) ? 0 : lastElement.id;
for (var i = removedIndex; i < list.length; i++) {
list[i].id = ++lastID;
}
}
return list;
};
Try the below method. Hope it works !!
// index of the deleted item
var itemDeleted = 2;
// create a for loop from deleted index till last
for (var i = itemDeleted-1; i < jsonObj.length; i++) {
jsonObj[i].id = i+1;
}
You can do in pure js by using map
const jsonObj = [{
'name': 'Ray',
'email': 'ray#gmail.com'
},
{
'name': 'Steve',
'email': 'steve#gmail.com'
},
{
'name': 'Albert',
'email': 'albert#gmail.com'
},
{
'name': 'Christopher',
'email': 'chris#gmail.com'
}
];
jsonObj.splice(1, 1);
const newObj = jsonObj.map((c, i) => ({
name: c.name,
email: c.email,
id: i + 1
}));
console.log(newObj);
Get the deleted index and assign it to the i value of for loop
for (var i=deletedIndex; i<jsonObj.length; i++) {
jsonObj[i].id--;
}

Javascript | Search in an array of JSON by JSONs specific key value

I need search in an array of JSON objects if a key with especific id value exists. If exists, return it, if not return -1 or whatever
var array = [{'id': 1, 'name': 'xxx'},
{'id': 2, 'name': 'yyy'},
{'id': 3, 'name': 'zzz'}];
var searchValue --> id==1
should be something like this?
function search_array(array,valuetofind) {
if array.indexof({'id': valuetofind}) != -1 {
return array[array.indexof({'id': valuetofind})]
} else {
return {'id': -1}
}
}
This returns the object if a match exists and -1 if there's no match.
function search_array(array,valuetofind) {
for (i = 0; i < array.length; i++) {
if (array[i]['id'] === valuetofind) {
return array[i];
}
}
return -1;
}
If you simply need to make sure the id exists try this:
function search_array(array, valuetofind) {
var exists = false;
for(i=0;i<array.length;i++) {
if(array[i].id == valuetofind) {
exists = true;
}
}
return exists;
}
My method may be a little long winded cycling through each part but i checked and it works
search_array(array, 4) [False]
search_array(array, 1) [True]
try this
search(nameKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].name === nameKey) {
return myArray[i];
}
}
}
var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
var resultObject = search("string 1", array);

Getting index of array object element given multiple object keys

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

remove duplicated value from array and replace it with the latest value in javascript

I have an array in JavaScript. The user enters string and the data placed in this array in the form of value and name.
if(!_.isUndefined(args[1]) && !_.isUndefined(args[2])) {
if(args[1].length !== 0 && args[2].length !== 0) {
var dataObj = {
name : args[1],
value : args[2]
};
formateArray.push({name: dataObj.name, value:dataObj.value});
How can I remove duplicated value from array and replace it with the latest value the user enters?
So when the user enters: value_1 100, value_2 200, value_1 500
I expect to see: value_1 500, value_2 200 (replace the duplicates with new data)
You can iterate your array replace the value if the name already exists.
function push(array, newVal) {
var found = false;
for (var i = 0; i < array.length && !found; i++) {
if (array[i].name === newVal.name) {
array[i].value = newVal.value;
found = true;
}
}
if (!found) {
array.push(newVal);
}
}
function printNameValue(array) {
var out = '';
for (var i = 0; i < array.length; i++) {
out += array[i].name + ' ' + array[i].value + ', ';
}
return out;
}
var myArray = [];
push(myArray, {
name: 'value_1',
value: 100
});
push(myArray, {
name: 'value_2',
value: 200
});
push(myArray, {
name: 'value_1',
value: 500
});
alert(printNameValue(myArray));
Since your values can be associated with meaningful keys, perhaps you should use an object map rather than an array to store your values. Avoiding duplicates now becomes trivial since you cannot have duplicate keys.
var valuesMap = {};
//setting value
valuesMap.value_1 = 100;
//setting another value
valuesMap.value_2 = 200;
//replacing it
valuesMap.value_1 = 500;
Otherwise it's still quite simple, but less efficient:
function add(arr, obj) {
var key = obj.name, i, len;
for (i = 0, len = arr.length; i < len; i++) {
if (arr[i].name === key) {
arr[i] = obj;
return;
}
}
arr.push(obj);
}
var values = [];
add(values, { name: 'test', value: 1 });
add(values, { name: 'test', value: 2 });
values.length; //1
Instead of the array object, i suggest you to use an object that will act like a hashtable. You can define on this way var formateArray = {};
When you want to add or edit the data, instead of using push, you can do it like this:
formateArray[dataObj.name] = {name: dataObj.name, value:dataObj.value};
If the key does not exist dataObj.name, it will be added. It the key exist, the value would set with the new value.
If you want the size of you array, you get it this way Object.keys(formateArray).length
If you want to loop on your data, you can do it this way:
for (var k in formateArray) {
// use hasOwnProperty to filter out keys from the Object.prototype
if (formateArray.hasOwnProperty(k)) {
alert('key is: ' + k + ', value is: ' + formateArray[k].value);
}
}
Here is a jsfiddle that illustrate this.

Categories

Resources