Index of array containing key value pair in JSON file - javascript

How would I write a function that searches a JSON file to find the index where there exists an object containing a particular key-value pair? For example, the function would return 0 for the following JSON document if we were looking for value: '100.0' because key1 rests at index 0.
var object = {
key1 : {
name : 'xxxxxx',
value : '100.0'
},
key2 : {
name : 'yyyyyyy',
value : '200.0'
},
key3 : {
name : 'zzzzzz',
value : '500.0'
},
}

Here's a little fiddle for you:
http://jsfiddle.net/qskt9Lg9/
function findInJson(key1, value) { //pass it the desired matching key value pairs
var i = 0;
for (var key in object) { //this will iterate through key1 - key3
var current = object[key];
if (current[key1] == value) {
return i; //return the index
}
i++;//increment if no found
}
return -1;
}
and you would just call it like so:
findInJson("name", "xxxxxx")
This site: http://crunchify.com/how-to-iterate-through-jsonarray-in-javascript/ gives you a lot more info on how to parse JSON.

If it is guaranteed a key in your object will not be deleted and added later you can use:
function (object, key, value) {
var index = 0;
for (var oKey in object) {
if (object[oKey][key] === value) {
return index;
}
index++;
}
return -1;
}
But for..in statement iterates over an object in an arbitrary order. See: "Deleted, added or modified properties" section for detailed explanation.
It would be better to use an array and fit "keyN" into your {name, value} object if you want the order to be preserved.

If you are not interested in supporting IE8 and below, then you could use the Object.keys() method like :
function getIndex(val) {
for (i = 0; i < Object.keys(object).length; i++) {
if (object[Object.keys(object)[i]].value === val) {
return i;
}
};
return -1;
};
console.log( getIndex("100.0") ); // returns 0
console.log( getIndex("500.0") ); // returns 2
console.log( getIndex("800.0") ); // returns -1
See JSFIDDLE

Related

Unable to add key-value pair in Javascript Object after sorting them

Hey I have an object as
data = { vaibhavs:243 "132" ,
vaibhavs:918 "342",
karthikp:265 "345",
marimuthu:848 "asjkd",
ushas:918 "sdf15",
apoorvg:728 "wddfs"....
}
I need to sort it on the basis of key ,i.e., "vaibhavs:243" alphabatically.
I have written the following code for it. I am able to sort the keys, but not able to put it back in the returning object. Please guide what to do.
function sortData(data) {
var sorted = [];
Object.keys(data).sort(function (a, b) {
return a < b ? -1 : 1
}).forEach(function (key) {
var obj = {};
obj[key] = data[key]
sorted.push(obj);
});
return sorted;
}
This is giving me an array of object which i don't want. I want a single object.
I can't do this using an array because I need the sorted key-value pairs.
If I am using the following code,
function sortData(data) {
var sorted = {};
Object.keys(data).sort(function (a, b) {
return a < b ? -1 : 1
}).forEach(function (key) {
sorted[key] = key[data]
});
return sorted;
}
The above code is always overwriting the existing value. Can someone suggest me how to add the Key value pair. As I have that in sorted order, but just unable to add them in a single object.

How to compare JSON object structure, compare [{},{},{}] vs {k:v, k2:v2,...}

In my PHP app, for 2 different tools I get 2 json responses but I would like to use the same javascript to handle both.
All I need to do is to compare this structure
data = [
{'k1':'v1'},
{'k2':'v2'},
{'k3':'v3'}
]
with this one:
data = {'k11':'v11', 'k22':'v22', 'k33':'v33'}
this must work for any number of results (they are not suppose to match) and it also needs to work if 1 result is given in any of both cases.
I've already tried
- using data.length (which I thought in the second case would give me 1 array
- data instanceof Array, which is true for both cases (same thing if I do data instanceof Object)
What is the best approach to compare these in javascript?
EDITED: the keys and values of both json are not suppose to match, I only want to compare the structure, or detect one of them without having the other. (array of objects vs object with properties)
The first one is an array of objects; the second is an object with properties.
If you need to do this on the PHP side, just do json_decode($data, true) on the second data structure (the second argument casts it as an array) before output. You may have to play around with it a little to get the structures to line up but it should be pretty straight-forward.
Otherwise, in js -
var data = [
{'k':'v'},
{'k2':'v2'},
{'k3':'v3'}
],
data2 = {'k':'v', 'k2':'v2', 'k3':'v3'},
data3 = [];
for (var x in data2) {
data3.push({x:data2[x]});
}
To check if the received data is in the first format or the second one you may just check if the data is an array or not.
The correct way to do it is as follows:
if( Object.prototype.toString.call( data ) === '[object Array]' )
{
// an array: parse data according to first format
}
else
{
// not an array: parse data according to second format
}
There are several way you can do this. I would flatten the array so that it matches the format of the object:
function Flatten(source) {
if (!source || !source.length || source.length === 0) {
return source;
}
var output = {};
for (var i = 0; i < source.length; i++) {
for (prop in source[i]) {
if (source[i].hasOwnProperty(prop)) {
output[prop] = source[i][prop];
}
}
}
return output;
}
Here's a fiddle
I'm not actually sure what you mean by "compare", but if you want a purely JavaScript solution to finding out if the structures are equivalent, you could use this.
Simple Shallow Solution
function numOwnProperties (obj) {
var i = 0;
for (var key in obj) {
if (obj.hasOwnProperty(key)) { i++; }
}
return i;
}
function areEquivalent (obj, arr) {
var length = arr.length;
if (numOwnProperties(obj) !== length) { return false; }
for (var i = 0; i < length; i++) {
var item = arr[i];
for (var key in item) {
if (item.hasOwnProperty(key) && item[key] !== obj[key]) {
return false;
}
}
}
return true;
};
Usage
data1 = [
{'k':'v'},
{'k2':'v2'},
{'k3':'v3'}
];
data2 = {
'k':'v',
'k2':'v2',
'k3':'v3'
};
console.log(areEquivalent(data2, data1)); // true

How to use Underscore.js filter with an object?

I have an object like so:
> Object
> Rett#site.com: Array[100]
> pel4#gmail.com: Array[4]
> 0
id : 132
selected : true
> 1
id : 51
selected : false
etc..
How can I use the underscore _.filter() to return back only the items where selected === true?
I've never had the need to go down to layers with _.filter(). Something like
var stuff = _.filter(me.collections, function(item) {
return item[0].selected === true;
});
Thank you
If you want to pull all array elements from any e-mail address where selected is true, you can iterate like so:
var selected = [];
for (email in emailLists) {
selected.concat(_.filter(emailLists[email], function (item) {
return item.selected === true;
}));
}
If you only want to pull the arrays where all elements are selected, you might instead do something like this:
var stuff = _.filter(me.collections, function(item) {
return _.all(item, function (jtem) {
jtem.selected === true;
});
});
Underscore's filter method will work on an object being used as a hash or dictionary, but it will return an array of the object's enumerable values and strip out the keys. I needed a function to filter a hash by its values that would preserve the keys, and wrote this in Coffeescript:
hash_filter: (hash, test_function) ->
keys = Object.keys hash
filtered = {}
for key in keys
filtered[key] = hash[key] if test_function hash[key]
filtered
If you're not using Coffeescript, here's the compiled result in Javascript, cleaned up a little:
hash_filter = function(hash, test_function) {
var filtered, key, keys, i;
keys = Object.keys(hash);
filtered = {};
for (i = 0; i < keys.length; i++) {
key = keys[i];
if (test_function(hash[key])) {
filtered[key] = hash[key];
}
}
return filtered;
}
hash = {a: 1, b: 2, c: 3};
console.log((hash_filter(hash, function(item){return item > 1;})));
// Object {b=2, c=3}
TL; DR: Object.keys() is great!
I have an object called allFilterValues containing the following:
{"originDivision":"GFC","originSubdivision":"","destinationDivision":"","destinationSubdivision":""}
This is ugly but you asked for an underscore based way to filter an object. This is how I returned only the filter elements that had non-falsy values; you can switch the return statement of the filter to whatever you need:
var nonEmptyFilters = _.pick.apply({}, [allFilterValues].concat(_.filter(_.keys(allFilterValues), function(key) {
return allFilterValues[key];
})));
Output (JSON/stringified):
{"originDivision":"GFC"}
#Dexygen was right to utilize _.pick but a cleaner solution is possible because the function also accepts a predicate
Return a copy of the object, filtered to only have values for the allowed keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick.
(highlight is mine)
Here's a real life example I've used in a project
_.pick({red: false, yellow: true, green: true}, function(value, key, object) {
return value === true;
});
// {yellow: true, green: true}
const obj = {
1 : { active: true },
2 : { active: false },
3 : { active: false },
}
let filtered = Object.entries(obj).reduce((acc, current) => {
const currentEntry = current[1];
const currentKey = current[0];
//here you check condition
if (currentEntry.active) {
return {
...acc,
[currentKey]: currentEntry
}
}
return acc;
}, {})
There is a rule of thumb, if you need to achieve something really exotic look up into reducer it can solve almost all problems related to objects, it's a bit tricky to get used to it, but trust me thorough reading of documentation gonna pay off.
Maybe you want a simplest way
_.filter(me.collections, { selected: true})

Getting the first index of an object

Consider:
var object = {
foo: {},
bar: {},
baz: {}
}
How would I do this:
var first = object[0];
console.log(first);
Obviously, that doesn’t work because the first index is named foo,
not 0.
console.log(object['foo']);
works, but I don’t know it’s named foo. It could be named anything. I just want the first.
Just for fun this works in JS 1.8.5
var obj = {a: 1, b: 2, c: 3};
Object.keys(obj)[0]; // "a"
This matches the same order that you would see doing
for (o in obj) { ... }
If you want something concise try:
for (first in obj) break;
alert(first);
wrapped as a function:
function first(obj) {
for (var a in obj) return a;
}
they're not really ordered, but you can do:
var first;
for (var i in obj) {
if (obj.hasOwnProperty(i) && typeof(i) !== 'function') {
first = obj[i];
break;
}
}
the .hasOwnProperty() is important to ignore prototyped objects.
This will not give you the first one as javascript objects are unordered, however this is fine in some cases.
myObject[Object.keys(myObject)[0]]
If the order of the objects is significant, you should revise your JSON schema to store the objects in an array:
[
{"name":"foo", ...},
{"name":"bar", ...},
{"name":"baz", ...}
]
or maybe:
[
["foo", {}],
["bar", {}],
["baz", {}]
]
As Ben Alpert points out, properties of Javascript objects are unordered, and your code is broken if you expect them to enumerate in the same order that they are specified in the object literal—there is no "first" property.
for first key of object you can use
console.log(Object.keys(object)[0]);//print key's name
for value
console.log(object[Object.keys(object)[0]]);//print key's value
There is no way to get the first element, seeing as "hashes" (objects) in JavaScript have unordered properties. Your best bet is to store the keys in an array:
var keys = ["foo", "bar", "baz"];
Then use that to get the proper value:
object[keys[0]]
ES6
const [first] = Object.keys(obj)
Using underscore you can use _.pairs to get the first object entry as a key value pair as follows:
_.pairs(obj)[0]
Then the key would be available with a further [0] subscript, the value with [1]
I had the same problem yesterday. I solved it like this:
var obj = {
foo:{},
bar:{},
baz:{}
},
first = null,
key = null;
for (var key in obj) {
first = obj[key];
if(typeof(first) !== 'function') {
break;
}
}
// first is the first enumerated property, and key it's corresponding key.
Not the most elegant solution, and I am pretty sure that it may yield different results in different browsers (i.e. the specs says that enumeration is not required to enumerate the properties in the same order as they were defined). However, I only had a single property in my object so that was a non-issue. I just needed the first key.
You could do something like this:
var object = {
foo:{a:'first'},
bar:{},
baz:{}
}
function getAttributeByIndex(obj, index){
var i = 0;
for (var attr in obj){
if (index === i){
return obj[attr];
}
i++;
}
return null;
}
var first = getAttributeByIndex(object, 0); // returns the value of the
// first (0 index) attribute
// of the object ( {a:'first'} )
To get the first key of your object
const myObject = {
'foo1': { name: 'myNam1' },
'foo2': { name: 'myNam2' }
}
const result = Object.keys(myObject)[0];
// result will return 'foo1'
Based on CMS answer. I don't get the value directly, instead I take the key at its index and use this to get the value:
Object.keyAt = function(obj, index) {
var i = 0;
for (var key in obj) {
if ((index || 0) === i++) return key;
}
};
var obj = {
foo: '1st',
bar: '2nd',
baz: '3rd'
};
var key = Object.keyAt(obj, 1);
var val = obj[key];
console.log(key); // => 'bar'
console.log(val); // => '2nd'
My solution:
Object.prototype.__index = function(index)
{
var i = -1;
for (var key in this)
{
if (this.hasOwnProperty(key) && typeof(this[key])!=='function')
++i;
if (i >= index)
return this[key];
}
return null;
}
aObj = {'jack':3, 'peter':4, '5':'col', 'kk':function(){alert('hell');}, 'till':'ding'};
alert(aObj.__index(4));

How do I remove objects from a JavaScript associative array?

Suppose I have this code:
var myArray = new Object();
myArray["firstname"] = "Bob";
myArray["lastname"] = "Smith";
myArray["age"] = 25;
Now if I wanted to remove "lastname"?....is there some equivalent of
myArray["lastname"].remove()?
(I need the element gone because the number of elements is important and I want to keep things clean.)
Objects in JavaScript can be thought of as associative arrays, mapping keys (properties) to values.
To remove a property from an object in JavaScript you use the delete operator:
const o = { lastName: 'foo' }
o.hasOwnProperty('lastName') // true
delete o['lastName']
o.hasOwnProperty('lastName') // false
Note that when delete is applied to an index property of an Array, you will create a sparsely populated array (ie. an array with a missing index).
When working with instances of Array, if you do not want to create a sparsely populated array - and you usually don't - then you should use Array#splice or Array#pop.
Note that the delete operator in JavaScript does not directly free memory. Its purpose is to remove properties from objects. Of course, if a property being deleted holds the only remaining reference to an object o, then o will subsequently be garbage collected in the normal way.
Using the delete operator can affect JavaScript engines' ability to optimise code.
All objects in JavaScript are implemented as hashtables/associative arrays. So, the following are the equivalent:
alert(myObj["SomeProperty"]);
alert(myObj.SomeProperty);
And, as already indicated, you "remove" a property from an object via the delete keyword, which you can use in two ways:
delete myObj["SomeProperty"];
delete myObj.SomeProperty;
Hope the extra info helps...
None of the previous answers address the fact that JavaScript does not have associative arrays to begin with - there is no array type as such, see typeof.
What JavaScript has, are object instances with dynamic properties. When properties are confused with elements of an Array object instance then Bad Things™ are bound to happen:
Problem
var elements = new Array()
elements.push(document.getElementsByTagName("head")[0])
elements.push(document.getElementsByTagName("title")[0])
elements["prop"] = document.getElementsByTagName("body")[0]
console.log("number of elements: ", elements.length) // Returns 2
delete elements[1]
console.log("number of elements: ", elements.length) // Returns 2 (?!)
for (var i = 0; i < elements.length; i++)
{
// Uh-oh... throws a TypeError when i == 1
elements[i].onmouseover = function () { window.alert("Over It.")}
console.log("success at index: ", i)
}
Solution
To have a universal removal function that does not blow up on you, use:
Object.prototype.removeItem = function (key) {
if (!this.hasOwnProperty(key))
return
if (isNaN(parseInt(key)) || !(this instanceof Array))
delete this[key]
else
this.splice(key, 1)
};
//
// Code sample.
//
var elements = new Array()
elements.push(document.getElementsByTagName("head")[0])
elements.push(document.getElementsByTagName("title")[0])
elements["prop"] = document.getElementsByTagName("body")[0]
console.log(elements.length) // Returns 2
elements.removeItem("prop")
elements.removeItem(0)
console.log(elements.hasOwnProperty("prop")) // Returns false as it should
console.log(elements.length) // returns 1 as it should
That only deletes the object, but it still keeps the array length the same.
To remove the element from the array, you need to do something like:
array.splice(index, 1);
While the accepted answer is correct, it is missing the explanation why it works.
First of all, your code should reflect the fact that this is not an array:
var myObject = new Object();
myObject["firstname"] = "Bob";
myObject["lastname"] = "Smith";
myObject["age"] = 25;
Note that all objects (including Arrays) can be used this way. However, do not expect for standard JavaScript array functions (pop, push, etc.) to work on objects!
As said in accepted answer, you can then use delete to remove the entries from objects:
delete myObject["lastname"]
You should decide which route you wish to take - either use objects (associative arrays / dictionaries) or use arrays (maps). Never mix the two of them.
There is an elegant way in the Airbnb Style Guide to do this (ECMAScript 7):
const myObject = {
a: 1,
b: 2,
c: 3
};
const { a, ...noA } = myObject;
console.log(noA); // => { b: 2, c: 3 }
Copyright: https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90
As other answers have noted, you are not using a JavaScript array, but a JavaScript object, which works almost like an associative array in other languages except that all keys are converted to strings. The new Map stores keys as their original type.
If you had an array and not an object, you could use the array's .filter function, to return a new array without the item you want removed:
var myArray = ['Bob', 'Smith', 25];
myArray = myArray.filter(function(item) {
return item !== 'Smith';
});
If you have an older browser and jQuery, jQuery has a $.grep method that works similarly:
myArray = $.grep(myArray, function(item) {
return item !== 'Smith';
});
Use method splice to completely remove an item from an object array:
Object.prototype.removeItem = function (key, value) {
if (value == undefined)
return;
for (var i in this) {
if (this[i][key] == value) {
this.splice(i, 1);
}
}
};
var collection = [
{ id: "5f299a5d-7793-47be-a827-bca227dbef95", title: "one" },
{ id: "87353080-8f49-46b9-9281-162a41ddb8df", title: "two" },
{ id: "a1af832c-9028-4690-9793-d623ecc75a95", title: "three" }
];
collection.removeItem("id", "87353080-8f49-46b9-9281-162a41ddb8df");
You are using Object, and you don't have an associative array to begin with. With an associative array, adding and removing items goes like this:
Array.prototype.contains = function(obj)
{
var i = this.length;
while (i--)
{
if (this[i] === obj)
{
return true;
}
}
return false;
}
Array.prototype.add = function(key, value)
{
if(this.contains(key))
this[key] = value;
else
{
this.push(key);
this[key] = value;
}
}
Array.prototype.remove = function(key)
{
for(var i = 0; i < this.length; ++i)
{
if(this[i] == key)
{
this.splice(i, 1);
return;
}
}
}
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function ForwardAndHideVariables() {
var dictParameters = getUrlVars();
dictParameters.add("mno", "pqr");
dictParameters.add("mno", "stfu");
dictParameters.remove("mno");
for(var i = 0; i < dictParameters.length; i++)
{
var key = dictParameters[i];
var value = dictParameters[key];
alert(key + "=" + value);
}
// And now forward with HTTP-POST
aa_post_to_url("Default.aspx", dictParameters);
}
function aa_post_to_url(path, params, method) {
method = method || "post";
var form = document.createElement("form");
// Move the submit function to another variable
// so that it doesn't get written over if a parameter name is 'submit'
form._submit_function_ = form.submit;
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var i = 0; i < params.length; i++)
{
var key = params[i];
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form._submit_function_(); // Call the renamed function
}
If, for whatever reason, the delete key is not working (like it wasn't working for me), you can splice it out and then filter the undefined values:
// To cut out one element via arr.splice(indexToRemove, numberToRemove);
array.splice(key, 1)
array.filter(function(n){return n});
Don’t try and chain them since splice returns removed elements;
By using the "delete" keyword, it will delete the array element from array in JavaScript.
For example,
Consider following statements.
var arrayElementToDelete = new Object();
arrayElementToDelete["id"] = "XERTYB00G1";
arrayElementToDelete["first_name"] = "Employee_one";
arrayElementToDelete["status"] = "Active";
delete arrayElementToDelete["status"];
The last line of the code will remove the array element whose key is "status" from the array.
You can do the following if you want a more functional and elegant approach:
const o = { firstName: "foo", lastName: "bar" };
const { lastName, ...removed } = o;
lastName // bar
removed // { firstName: "foo" }
Note that the value of removed will be undefined if there are no items left in the object.
You can remove an entry from your map by explicitly assigning it to 'undefined'. As in your case:
myArray["lastname"] = undefined;
We can use it as a function too. Angular throws some error if used as a prototype. Thanks #HarpyWar. It helped me solve a problem.
var removeItem = function (object, key, value) {
if (value == undefined)
return;
for (var i in object) {
if (object[i][key] == value) {
object.splice(i, 1);
}
}
};
var collection = [
{ id: "5f299a5d-7793-47be-a827-bca227dbef95", title: "one" },
{ id: "87353080-8f49-46b9-9281-162a41ddb8df", title: "two" },
{ id: "a1af832c-9028-4690-9793-d623ecc75a95", title: "three" }
];
removeItem(collection, "id", "87353080-8f49-46b9-9281-162a41ddb8df");
It's very straightforward if you have an Underscore.js dependency in your project -
_.omit(myArray, "lastname")
The only working method for me:
function removeItem (array, value) {
var i = 0;
while (i < array.length) {
if(array[i] === value) {
array.splice(i, 1);
} else {
++i;
}
}
return array;
}
Usage:
var new = removeItem( ["apple","banana", "orange"], "apple");
// ---> ["banana", "orange"]
For "Arrays":
If you know the index:
array.splice(index, 1);
If you know the value:
function removeItem(array, value) {
var index = array.indexOf(value);
if (index > -1) {
array.splice(index, 1);
}
return array;
}
The most upvoted answer for delete works well in case of objects but not for the real arrays. If I use delete it removes elements from loops but keeps the element as empty and length of array wont change. This may be a problem in some scenarios.
For example, if I do myArray.toString() on myArray after removal via delete, it creates an empty entry, i.e. ,,.
var myArray = newmyArray = new Object();
myArray["firstname"] = "Bob";
myArray["lastname"] = "Smith";
myArray["age"] = 25;
var s = JSON.stringify(myArray);
s.replace(/"lastname[^,}]+,/g, '');
newmyArray = JSON.parse(p);
Without looping/iterates we get the same result.

Categories