Add two json object data in javascript [duplicate] - javascript

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 8 years ago.
I want to add two JSON objects which have structure like this (It has more data and I ignore them in here):
{ "total_count":100 , "types":20 , { "a" : 5 , "b" : 6 }}
{"total_count" : 1 , { "a" : 2 } }
I want to add these objects in node.js in order to get following object:
{ "total_count":101 , "types":20 , { "a" : 7 , "b" : 6 }}
I use mongodb which has $inc function which add current json to existing one in db and reduce load and store load. I'm interested to find a method which handle this action in nodejs.What should I do ( I dont want to do it manually preferly. instead i like to use existing functions)?

something like this? your json structure is not quite correct, so i added x.
object1 = {"total_count":100 , "types":20 , "x": { "a" : 5 , "b" : 6 }};
object2 = {"total_count" : 1 , "x":{ "a" : 2 } };
object1.total_count += object2.total_count;
object1.x.a += object2.x.a;

Related

Nested array filtering [duplicate]

This question already has answers here:
Filter nested array in object array by array of values
(4 answers)
Closed 2 years ago.
categories.location array is filtered like this:
const displayedCategories = categories.filter(
(category) => category.location.indexOf(selectedLocation) >= 0
);
Now its structure is changed like below, there's additional nested values. How can I filter value/label now? category.location.value.indexOf doesn't work. Thanks.
"location" : [
{
"value" : "London",
"label" : "London"
}
]
Try:
const displayedCategories = categories.filter(
(category) => category.location.some(loc=>loc.value==selectedLocation)
);

Sort an array of objects by a given property order [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Sorting an array of objects by property values
(35 answers)
Closed 4 years ago.
I have this array :
displayedColumns = ['CompanyName','Ticker', 'Id', 'Name', 'ActualTradeDate',
'Spot', 'PreviousTradeDate', 'PreviousSpot', 'FPrice', 'Status']
and an array of objects with the above values as properties :
data : [{Ticker : ".. " ......... Status : "... " } , .... {...} ]
but the properties in the data array are not sorted in the 'displayedColums' order (as wanted )
the end wanted result is :
data = [{ CompanyName : ".. " Ticker : ".."......Status : ".." },
{ CompanyName : ".. " Ticker : ".."......Status : ".." },....
{ CompanyName : ".. " Ticker : ".."......Status : ".." }]
**just to be clear, I am not looking to sort the array by asc/desc values of a certain property. I want to change the order of the properties in the array to 'displayedColumn' order
I am not sure how to sort an array by property order.
thanks a lot
Objects are never sorted, but arrays are. Therefore you could turn your array of objects into an array of arrays, containing the objects values in displayedColumns order:
const result = data.map(obj => displayedColumns.map(key => obj[key]));

Angular2 Typescript how to retrieve an array from inside an object [duplicate]

This question already has answers here:
Find a value in an array of objects in Javascript [duplicate]
(20 answers)
Closed 5 years ago.
When I do console.log of the following:
console.log(this.categories);
I get this structure:
which was created from this json from the api:
[
{
"id":"dff0bb3e-889f-43e6-b955-52cc7203952f",
"name":"Wood",
"category_types":[
{
"id":"49b31f43-d98d-43c8-9798-088ec6914fe5",
"name":"Solid"
},
{
"id":"8592716a-ffd5-4e97-ba9e-6a64ba6e48f1",
"name":"Engineered"
}
]
},
{
"id":"6b2b6914-6c64-4389-a327-be3f08fd066d",
"name":"Carpet",
"category_types":[
{
"id":"0e2c8473-90fb-4806-a6e7-2389eeb0f9e4",
"name":"Loop pile"
},
{
"id":"3c762653-4f0d-42d2-a84e-133c7051c95b",
"name":"Cut pile"
},
{
"id":"0997d4dc-e886-46ef-83b4-d90c4fb72553",
"name":"Cut \u0026 loop pile"
}
]
}
]
Given that I have the value 'Wood' in a string variable called value, how do I get hold of the category_types array for the Wood object?
console.log(this.categories[value]);
returns undefined.
You want to find object that has name of 'Wood', then get category_types. Array.prototype.find returns first value that meets the condition in the provided test function. In your case, it could look like this:
this.categories.find(value => value.name === 'Wood').category_types
You can use Array.filter:
let wood = this.categories.filter(category => {
return category.name === "Wood";
})[0];
This will filter your categories to retrieve the ones with name wood, then you take the first one. If you didn't find anything with name === "Wood" in your array, then it will be undefined.

accessing certain rows in json in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
guys i know it is dummy question but i spent a lot of hours and cant reach .. i ha ve json array and i want for example to access the elements of second row which are lname2 fname2 90 feedback2 .. this is the array var json = [ {
"lname" : "lname1",
"fname" : "fname1",
"age" : 10,
"feedback" : "feedback1"
}, {
"lname" : "lname2",
"fname" : "fname2",
"age" : 90,
"feedback" : "feedback2"
}, {
"lname" : "lname3",
"name" : "fname3",
"age" : 30,
"feedback" : "feedback3"
}
You have an array of objects - so iterate the array, access the object properties:
for (var i = 0; i < json.length; i++) {
console.log(json[i].fname);
}
To specifically access the second element of the array, specify the index:
var fname = json[1].fname;
You can access 2nd row by index
json[1]
From what I can see, this variable is not a JSON, but simply a javascript array of objects. To access a data in it, you'll have to use json[X].lname for exemple, where X is the index of the object you want to reach.
A JSON is a string of characters representing different type of data, such as arrays and objects. To decode such a string, you could use JSON.parse().
You can find more informations about JSON here : http://www.json.org/
Hope this helps!
Use the below code
json[1].lname
json[1].fname
json[1].age
json[1].feedback
Also what ever you have posted is not a JSON its an parsed result of JSON. JSON is ultimately a string. all the data is converted into one big string. Later when you parse the json you will get the result as in your post.
Explaining the code
Note that variable json is like var json = [] , this [ ] is used to create a array, Hence the variable json is an array and we can access the elements by inex
Here [1] referes to the index of the array. As your json is an array. We use index numbers to access the elements in the array. And the indexes start from 0. So your second element will be in the index 1, 3rd will be in 2 and so on...
Now json[1] will return the second element so..
json[1] = {
"lname" : "lname2",
"fname" : "fname2",
"age" : 90,
"feedback" : "feedback2"
}
note the { } this refers to an object, this { } is used to create an object. Now to access the property values of the object we use the . operator. hence the code json[1].lname , json[1].fname and so on..

jQuery - Search in Array of Objects [duplicate]

This question already has answers here:
JSON find in JavaScript
(7 answers)
Closed 7 years ago.
Assume I have an Array of jQuery objects. I would like to search in array and find out such a key exists are not.
NOTE: The objects are not of same pattern
Ex:
a = [ { "chart_type" : 4},
{ "x_axis" : { "field_type" : 5, "name" : "priority" } },
{ "y_axis" : { "field_type" : 3, "name" : "created_at" }}
]
I would like to search from the above, if key such as "chart_type" exists or not. If yes, I would like to get the value.
a.find("chart_type").value // Need something like this
I tried the following.
jQuery.grep(chart_hash, function(key,value){
if(key == "chart_type")
return value
});
However, the above doesn't work. Please help.
Use jQuery.each
$.each(chart_hash, function(index,value){
$.each(value,function(key,val){
if(key == "chart_type")
return val
});
})
})

Categories