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);
Related
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 5 months ago.
I have this data (data1) below, an array of objects with multiple objects inside:
[
{
"id": 35,
"code": "BP S",
"article": "BP S",
"price": 100,
"vat": null,
"status": null,
"company_id": 12
},
{
"id": 36,
"code": "B P S",
"article": "BPS",
"price": 100,
"vat": null,
"status": null,
"company_id": 12
}
]
I would like to extract the code of the first id=35.
How to do that?
My code:
import { useLocation } from "react-router-dom";
const Single = ({ inputs, title }) => {
const { state } = useLocation();
let data1 = state.foo;
console.log(data1);
You could use the High order function find to retrieve the item of the array matching the id = 35 , as in:
const item = data1.find((el)=>el.id===35)
Then you could extract the code with:
item.code
Or you could chain them:
const code = data1.find((el)=>el.id===35).code
This question already has answers here:
How to combine an array in javascript
(3 answers)
Closed 4 years ago.
I have a dataset which uses the same _id for two different objects and repeats this in the entire file. How do I go through the list of objects, find the two objects that have a matching _id and merge them into one object?
[
{
"_id": "591323037ca83d48eac1ff31",
"sessionStartTime": "2017-05-09T23:10:40.000Z",
"sessionEndTime": "2017-05-10T07:28:40.000Z",
"timeSessionMinutes": 212,
},
{
"_id": "591323037ca83d48eac1ff31",
"eventSummary": "Working",
"eventActivity": "Work",
"eventStart": "2017-05-09T10:00:00+02:00",
"eventEnd": "2017-05-09T17:00:00+02:00"
},
{
"_id": "5917165b3ffac25462193490",
"sessionStartTime": "2017-05-12T22:06:09.000Z",
"sessionEndTime": "2017-05-13T06:12:09.000Z",
"timeSessionMinutes": 322,
},
{
"_id": "5917165b3ffac25462193490",
"eventSummary": "Traveling back home",
"eventActivity": "Travel",
"eventStart": "2017-05-09T17:00:00+02:00",
"eventEnd": "2017-05-09T17:30:00+02:00"
},
...
]
Sorry if already answered, could not find a solution for this specific use case.
An alternative is using the function reduce to group the objects by _id and the function Object.values to extract the grouped objects.
let arr = [ { "_id": "591323037ca83d48eac1ff31", "sessionStartTime": "2017-05-09T23:10:40.000Z", "sessionEndTime": "2017-05-10T07:28:40.000Z", "timeSessionMinutes": 212, }, { "_id": "591323037ca83d48eac1ff31", "eventSummary": "Working", "eventActivity": "Work", "eventStart": "2017-05-09T10:00:00+02:00", "eventEnd": "2017-05-09T17:00:00+02:00" }, { "_id": "5917165b3ffac25462193490", "sessionStartTime": "2017-05-12T22:06:09.000Z", "sessionEndTime": "2017-05-13T06:12:09.000Z", "timeSessionMinutes": 322, }, { "_id": "5917165b3ffac25462193490", "eventSummary": "Traveling back home", "eventActivity": "Travel", "eventStart": "2017-05-09T17:00:00+02:00", "eventEnd": "2017-05-09T17:30:00+02:00" }],
result = Object.values(arr.reduce((a, c) => {
Object.assign((a[c['_id']] || (a[c['_id']] = Object.create(null))), c);
return a;
}, Object.create(null)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
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 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:
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.
}