This question already has answers here:
How can I get the index of an object by its property in JavaScript?
(22 answers)
Closed 1 year ago.
I have an array that looks like this:
const arr = [
{ "-MrUU6N2pi7aCwJoqCzP": { "name": "test", "brand": "test", "id": 1 } },
{ "-MrUUB4PcPD5G45NGb-y": { "name": "test2", "brand": "test2", "id": 2 } }
]
How do I find the index of the object with the key "-MrUUB4PcPD5G45NGb-y" for example?
You can use findIndex() and look at the Object.keys() for each object in the array to see if the key you want is in that keys array
const arr = [
{"-MrUU6N2pi7aCwJoqCzP":{"name":"test","brand":"test","id":1}},
{"-MrUUB4PcPD5G45NGb-y":{"name":"test2","brand":"test2","id":2}}
],
keyWanted = "-MrUUB4PcPD5G45NGb-y",
idx = arr.findIndex(e => Object.keys(e).includes(keyWanted))
console.log('Index =', idx)
Related
This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 8 months ago.
I have tried splicing my json file using a for loop
JSON file:
[
{
"name":"Billy Jean",
"age":"52",
"sex":"F",
},
{
"name":"Bob Semple",
"age":"32",
"sex":"M",
} there are more....
]
What I have tried (i imported it and called it contactList)
for(let i = 0 ; i < contactList.length ; i++){
if(contactlist.age > 40) {
contactList.splice(i, 1);
}
}
if i run the code and check the output nothing changes in my JSON file
You can create a new array using Array.prototype.filter() combined with Destructuring assignment
Notice that age property it's of type string and should be compared as number using unary plus (+) operator
Code:
const data = [{
"name": "Billy Jean",
"age": "52",
"sex": "F",
},
{
"name": "Bob Semple",
"age": "32",
"sex": "M",
}
]
const result = data.filter(({ age }) => +age > 40)
console.log(result)
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
I wanted to map all the names and power of the objects and if he finds an array inside gadgets he would add +1 (i++), how would that be?
The list is much bigger, but I just show these two
"list": [
{
"name": "SHELLY",
"power": 10,
"gadgets": [
{
"id": 23000255,
"name": "FAST FORWARD"
}
]
},
{
"name": "COLT",
"power": 7,
"gadgets": [
{
"id": 23000273,
"name": "SPEEDLOADER"
},
{
"id": 23000319,
"name": "SILVER BULLET"
}
]
]
}
A simple map should do it:
const data = {list:[{name:"SHELLY",power:10,gadgets:[{id:23000255,name:"FAST FORWARD"}]},{name:"COLT",power:7,gadgets:[{id:23000273,name:"SPEEDLOADER"},{id:23000319,name:"SILVER BULLET"}]}]};
const res = data.list.map(p => ({
name: p.name,
power: p.power + p.gadgets.length
}));
console.log(res);
This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Sort an array of object by a property (with custom order, not alphabetically)
(7 answers)
Closed 4 years ago.
Let's say I have the following array:
[
{
"key":"Certified?",
"value":"Yes"
},
{
"key":"Language",
"value":"EN"
},
{
"key":"Training-Place",
"value":"City"
}
]
I want to sort it in a specific order of Language, Certified?, Training-Place, how can I achive that? Preferably I would be able to define the sorting like:
["Language", "Certified?", "Training-Place"]
Thank you very much, any help is appreciated.
You can simply use Arrays.sort() and use a order array to sort the array in the order specified by the order array.
Try the following:
var arr = [ { "key":"Certified?", "value":"Yes" }, { "key":"Language", "value":"EN" }, { "key":"Training-Place", "value":"City" }, { "key":"Training-Place", "value":"aaa" }, { "key":"Language", "value":"bbb" }];
var order = ["Language", "Certified?", "Training-Place"];
arr.sort((a,b)=> order.indexOf(a.key) - order.indexOf(b.key));
console.log(arr);
Create an array which will contain the order. Now loop over this array and from the main array filter out the object which matches with the name
let arrayToSort = [{
"key": "Certified?",
"value": "Yes"
},
{
"key": "Language",
"value": "EN"
},
{
"key": "Training-Place",
"value": "City"
}
]
let sortOrder = ["Language", "Certified?", "Training-Place"]
let customSortArray = [];
sortOrder.forEach(function(item) {
customSortArray.push(
arrayToSort.filter(function(items) {
return item === items.key
})[0])
})
console.log(customSortArray)
This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 6 years ago.
I have a json array like this
[
{"Id":1,
"Name":"John"
},
{"Id":2,
"Name":"Mathew"
},
{"Id":3,
"Name":"Wilfred"
},
{"Id":4,
"Name":"Gary"
}
]
I need to implement an auto complete feature using this data.
so if I search for "Wil" I should get Wilfred as result. How can I do such a search similar to the SQL LIKE in JSON array
Use Array.prototype.filter
var persons = [{
"Id": 1,
"Name": "John"
}, {
"Id": 2,
"Name": "Mathew"
}, {
"Id": 3,
"Name": "Wilfred"
}, {
"Id": 4,
"Name": "Gary"
}]
var searchTerm = "Wil";
var results = persons.filter(function(person) {
return person.Name.indexOf(searchTerm) > -1;
});
console.log(results);
This question already has answers here:
Convert string in dot notation to get the object reference [duplicate]
(6 answers)
Closed 8 years ago.
How to check in an JSON object, the input path is present or not?
var obj = {
"schemaOne": {
"name": "abc",
"Path": "i.abc",
"count": 5347,
"subFolders": [
]
},
"schemaTwo": {
"name": "cde",
"Path": "i.cde",
"count": 0,
"subFolders": [
{
"name": "efg",
"Path": "",
"count": 0,
"subFolders": [
]
},
{
"name": "hij",
"Path": "i.hij",
"count": 1,
"subFolders": [
]
}
]
}
}
var inputpath = "obj.count";
After doing several research I came across below code. Here in this code, o.Path is known to the user. But I want to modify the code so tat dynamically check obj.count is present in JSON object or not?
function upd(o, path, count) {
if (o.Path == path) {
o.count = count;
} else {
var arr;
if (Array.isArray(o)) arr = o;
else if (o.subFolders) arr = o.subFolders;
else return;
for(var j=0; j < arr.length; j++) {
upd(arr[j], path, count);
}
}
}
Your question isn't very clear, neither the use case. As someone told u, obj is a JavaScript object, not a JSON.
If I understood correctly, you want to check if a path, espressed as string (such as "obj.count") exists in that object.
A very fast but not safe solution (eval is evil and should be avoided almost in every case) could be:
if(eval(inputpath)){
//... do something
}
In alternative, I suggest you to write a function that gets an object and a string (path) as parameter, parses the string path and check if the object contains such path.
Start from here: Accessing nested JavaScript objects with string key