Find the length of JSON object for identical key values [duplicate] - javascript

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
}

Related

How do I create an unique dataset of json object literal in Javascript?

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.

map and find the names of all objects [duplicate]

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);

Getting a particular attribute from a JSON data [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I have this result JSON data for my api call, but when i try to access the data in the attribute "69106658_5" I cant, I am getting "Error: Uncaught SyntaxError: Invalid or unexpected token". I have a copy of what I am running on an online editoe below. I am guessing its because of the attribute contains an underscore.
let results=
{
"links": {
"data": {
"self": {
"body": "",
"content_type": "",
"href": "/api/v2/nodes/69107289/categories",
"method": "GET",
"name": ""
}
}
},
"results": [
{
"data": {
"categories": {
"58652374_10": [
"16",
"16.0.1",
"16.2",
"16.2.4"
],
"58652374_11": [
"English"
],
"58652374_12": [
"Windows"
],
"58652374_13": "2018-11-20T00:00:00",
"58652374_2": "Published",
"58652374_3": "19",
"58652374_4": "Video",
"58652374_5": "65",
"58652374_6": "How To",
"58652374_7": [
"basic"
],
"58652374_8": "237",
"58652374_9": "Content Server"
}
}
},
{
"data": {
"categories": {
"69106658_2": "You Tube",
"69106658_3": [
"End User"
],
"69106658_4": [
"69106508:7"
],
"69106658_5": "https://img.youtube.com/vi/j-aOeCpRvEs/hqdefault.jpg",
"69106658_6": false,
"69106658_7": "Engineering",
"69106658_8": null
}
}
}
]
}
var lookInto = results.results;
for( let key in lookInto ) {
var selectData = lookInto[key].data.categories;
console.log(selectData);
}
console.log( selectData.69106658_5 )
Attribute fields that begin with anything other than a letter (and some symbols like _), you have to use bracket notation to access.
Instead of selectData.69106658_5, try selectData['69106658_5']
The underscore shouldn't cause any problem.
If you want to access the property "69106658_5", you should do like this :
results.results[1].data.categories["69106658_5"]

How to Combine Multiple Objects by id [duplicate]

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; }

How to implement search inside a json Array [duplicate]

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);

Categories

Resources