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);
Related
I want to get an output as an unique set of Categories array with the following output [Men,Woman].
Is there any way to do it in Javascript?
For example this my data
{
"products:"[
{
"id": 1,
"categories": {
"1": "Men",
},
},
{
"id": 2,
"categories": {
"1": "Men",
},
}, {
"id": 3,
"categories": {
"1": "Woman",
},
}
];
}
A simple 1 line answer would be
new Set(input.products.map(p => p.categories["1"]))
This is if you're expecting only key "1" in the categories object.
If it can have multiple categories then you can always do
const uniqueCategories = new Set();
input.products.forEach(p => uniqueCategories.add(...Object.values(p.categories)))
Now you can convert a Set into an array
PS: This is not a ReactJs problem but a pure JS question. You might want to remove the ReactJs tag from this question altogether.
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:
Self-references in object literals / initializers
(30 answers)
Closed 4 years ago.
I've created an array of object and in some of these objects I need to refer to the properties of that object inside of it like this:
let fields = [
{
"ATTRIBUTE_NAME": "PERSON_NAME",
"FIELD_NAME": "name"
"ATTRIBUTE_ID": 1,
"RULES": [
{
"MSG":`${this.ATTRIBUTE_NAME} is not valid`,
"NAME": "VALID_VALUES",
}
]
},
{
"ATTRIBUTE_NAME": "PERSON_JOB",
"FIELD_NAME": "job"
"ATTRIBUTE_ID": 2,
"RULES": [
{
"MSG":`${this.ATTRIBUTE_NAME} is not valid`,
"NAME": "VALID_VALUES",
}
]
}
]
But this keyword returning undefined! I want the "MSG" property to pick up the attribute name and populate it for me. Does anybody know how to capture it inside the template string?
The easiest way would be to use a getter for your RULES property.
A simple demonstration:
var obj = {
id: 1,
get info() {
return `The id is ${this.id}.`;
}
}
console.log(obj.info);
Applied to your code:
let fields = [{
"ATTRIBUTE_NAME": "PERSON_NAME",
"FIELD_NAME": "name",
"ATTRIBUTE_ID": 1,
get RULES() {
return [{
"MSG": `${this.ATTRIBUTE_NAME} is not valid`,
"NAME": "VALID_VALUES",
}]
}
}];
console.log(fields[0].RULES[0].MSG);
You can't use a self-reference like that, because the fields array and its sub-elements have not yet been declared. What you can do, instead, is:
Declare the fields array and its sub-elements first.
Then, iterate over every element and add the object you want in the RULES array.
Example:
/* Declare the array and its sub-elements. */
let fields = [{
"ATTRIBUTE_NAME": "PERSON_NAME",
"FIELD_NAME": "name",
"ATTRIBUTE_ID": 1,
"RULES": []
},
{
"ATTRIBUTE_NAME": "PERSON_JOB",
"FIELD_NAME": "job",
"ATTRIBUTE_ID": 2,
"RULES": []
}
];
/* Iterate over every element. */
fields.forEach(element => {
element.RULES.push({
"MSG": element.ATTRIBUTE_NAME + " is not valid",
"NAME": "VALID_VALUES"
});
});
/* Log the result. */
console.log(fields);
This question already has answers here:
How to count duplicate value in an array in javascript
(35 answers)
Closed 5 years ago.
I have a JSON file. I want to find the length of the JSON object where one key-value pair is similar. Like,
https://api.myjson.com/bins/h5mgv
[
{
"receive_date": "2013-11-04",
"responses": "2",
"name": "west"
},
{
"receive_date": "2013-11-04",
"responses": "8668",
"name": "west"
},
{
"receive_date": "2013-11-13",
"responses": "121",
"name": "east"
}
]
In the above example, length is 2 where "name": "west" and length is 1 where "name": "east" . I want to iterate through the JSON and find the identical values for the key name using Javascript. Output should look like,
east : 1
west : 2
By using length() I can find the length of whole JSON but what is recommended way to find the length for identical key values.
You can use reduce to get a new object listing the count of each name:
const myArray = [
{
"receive_date": "2013-11-04",
"responses": "2",
"name": "west"
},
{
"receive_date": "2013-11-04",
"responses": "8668",
"name": "west"
},
{
"receive_date": "2013-11-13",
"responses": "121",
"name": "east"
}
]
const myCounts = myArray.reduce((counts, item) => {
if (counts[item.name] === undefined) counts[item.name] = 0;
counts[item.name]++;
return counts;
}, {});
console.log(myCounts);
This produces the result:
{
"west": 2,
"east": 1
}
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 10 years ago.
Hi guys I am using parseJSON to parse this JSON string:
json = [
{
"Answers": [
{
"Responses": [
],
"AnswerID": 1,
"AnswerText": "Green"
},
{
"Responses": [
{
"ResponseID": 1,
"RespondingUser": null,
"ResponseDate": "\/Date(1351694241577)\/"
},
{
"ResponseID": 2,
"RespondingUser": null,
"ResponseDate": "\/Date(1351694245093)\/"
}
],
"AnswerID": 2,
"AnswerText": "Blue"
}
],
"QuestionID": 1,
"QuestionText": "Favourite colour?",
"ClosingDate": "\/Date(1351953058527)\/",
"AskingUser": null
}
]
var result = jQuery.parseJSON(json);
but how do I get the responses/response ID's out of 'result' now? Any help would be greatly appreciated!
[ ] = array
{ } = object
You have an array, lose the wrapping square brackets.
alert(json.Answers[0].AnswerText) = "Green"
You should be able to use the for-in loop:
for (i in result[0].Answers)
{
// do something with result[0].Answers[i].Responses
}
Is this what you're looking for?
for (var a in result[0].Answers) {
result[0].Answers[a].AnswerID // Do something with it.
}