I have json as below. what i am trying to do is calculate sum of price per each user
in this case for example, user001: 91.68
where I am in stuck is I am trying to write for loop to get those data from json but I have to assign direct usernumber(e.g. user001) to code.
I want to use that like shoppingJson[i][j] something like that
I only can get result when I write below
console.log(shoppingJson[0].user001[0].price)
how should I do to access 2d array values?
var shoppingJson = [{
"user001": [
{
"productId": "123",
"name": "Product 123",
"price": 14.23
},
{
"productId": "456",
"name": "Product 456",
"price": 4.56
},
{
"productId": "789",
"name": "Product 789",
"price": 72.89
}
]},{
"user002": [
{
"productId": "321",
"name": "Product 321",
"price": 3.21
},
{
"productId": "654",
"name": "Product 654",
"price": 61.54
},
{
"productId": "987",
"name": "Product 987",
"price": 59.87
}
]},{
"user003": [
{
"productId": "777",
"name": "Product 888",
"price": 4.213
},
{
"productId": "888",
"name": "Product 999",
"price": 6.24
},
{
"productId": "999",
"name": "Product 111",
"price": 9.71
}
]}
]
shoppingJson.forEach(function(obj) {
var totalPrice = 0;
for(key in obj) {
obj[key].forEach(function(o) {
totalPrice += o.price;
});
};
console.log(totalPrice);
});
You could use a forEach loop to iterate through each of the objects within the shoppingJson array. Then you can create a variable to store the total price, and it will be initialized to zero. For each key within the object you can do another forEach iteration (because the value of that key with be the array of objects), and add the value of "price" from that object to the totalPrice variable. If you try this on the first object in the shoppingJson array you get 91.68.
var price = {};
for(item of shoppingJson){
for( user in item ){
price[user] = 0;
for(product of item[user]){
price[user] += product.price;
}
}
}
After executing this, you will get the sums in the object price. The object price will then contain the following
{
user001: 91.68,
user002: 124.62,
user003: 20.163
}
Related
I am working on an angular application. I have an array as follows:
[{
"Name": "Andy"
},
{
"Name": "Bayer"
},
{
"Name": "James"
},
{
"Name": "Doda"
}]
I have another array which containes data as follows:
[
{
"Name": "Andy",
"Id": "1",
"Time": "2020-06-19T11:02+00:00"
},
{
"Name": "Billy",
"Id": "2",
"Time": "2020-06-19T11:05+00:00"
},
{
"Name": "Ciena",
"Id": 5
"Time": "2020-06-19T11:05+00:00"
},
{
"Name": "Doda",
"Id": "4",
"Time": "2020-06-19T11:05+00:00"
}
]
I want a resultant array such that code should check if Name is present in first array, then it should copy data from second array for that Name and push it in resultant array. For example common name between above two array is Andy and Doda, so data from Andy and Doda should be pushed to resultant array as follows:
[{
"Name": "Andy",
"Id": "1",
"Time": "2020-06-19T11:02+00:00"
},
{
"Name": "Bayer"
},
{
"Name": "James"
},
{
"Name": "Doda",
"Id": "4",
"Time": "2020-06-19T11:05+00:00"
}]
At run time I may get many names so code should be generic. I was trying following code which I got over stackoverflow itself
this.newArray = _.map(this.resultantArray, item => {
const value = _.find(this.dataArray, ['Name', item]);
const obj = value ? value : {Name: item};
return obj;
});
But this code is not working as expected as it works fine for the first time but when data comes for second time it appends data to previous data. I want array to be populated again freshly every time I send data. Please help
You can do this with vanilla JS no need for lodash. You can first map it and inside that you can find the value from second array otherwise return the current object:
var arrayTwo = [ { "Name": "Andy", "Id": "1", "Time": "2020-06-19T11:02+00:00" }, { "Name": "Billy", "Id": "2", "Time": "2020-06-19T11:05+00:00" }, { "Name": "Ciena", "Id": "5", "Time": "2020-06-19T11:05+00:00" }, { "Name": "Doda", "Id": "4", "Time": "2020-06-19T11:05+00:00" } ];
var arrayOne = [{ "Name": "Andy"}, { "Name": "Bayer"}, { "Name": "James"}, { "Name": "Doda"}];
var result = arrayOne.map(val=>arrayTwo.find(p=>p.Name==val.Name) || val);
console.log(result);
Suppose first array name is First
First : any [] = [{"Name": "Andy"},{"Name": "Bayer"},{ "Name": "James"},{"Name": "Doda"}]
And Second array name is Second
Second : any[] = [{"Name": "Andy","Id": "1","Time": "2020-06-19T11:02+00:00"},{"Name": "Bayer"},{"Name": "James"},{"Name": "Doda","Id": "4","Time": "2020-06-19T11:05+00:00"}]
Now do looping and check each name of first if its exists in second copy from second and push in result array
result : any[] =[];
this.First.forEach((element) => {
let index = this.Second.findIndex((x) => element.Name== x.Name);
if (index > -1) {
let data = {
this.Second[index].Name,
this.Second[index].Id,
this.Second[index].time,
};
this.result.push(data);
}
}
Here is my object:
var obj = {
"idtransact1": {
"amount": 3000,
},
"idtransact2": {
"amount": 3000,
}
}
I am trying to get the sum of all amount.
I tried to adapt this example but since it is not the same data structure then i am a bit lost.
var array = [{
"adults": 2,
"children": 3
}, {
"adults": 2,
"children": 1
}];
var val = array.reduce(function(previousValue, currentValue) {
return {
adults: previousValue.adults + currentValue.adults,
children: previousValue.children + currentValue.children
}
});
console.log(val);
Any help would be appreciated.
You can use Object.values() and .reduce() to get the sum:
const data = {
"idtransact1": { "amount": 3000 },
"idtransact2": { "amount": 3000 }
};
const result = Object.values(data).reduce((r, { amount }) => r + amount, 0);
console.log(result);
Using forEach loop
var obj = {
"idtransact1": {
"amount": 3000,
},
"idtransact2": {
"amount": 3000,
}
}
var sum=0;
Object.values(obj).forEach((x)=>sum+=x.amount)
console.log(sum)
A for in loop is your friend when it comes to looking for values in an object.
var obj = {
"idtransact1": {"amount": 3000},
"idtransact2": {"amount": 3000}};
var sumAmount = 0;
for(var char in obj){
sumAmount += obj[char].amount;
}
console.log(sumAmount);
For your second example, the for in loop works the same way with the array of objects.
var array = [
{"adults": 2,"children": 3},
{"adults": 2,"children": 1}];
var sumAdults = 0;
var sumChildren = 0;
for(var char in array){
sumAdults += array[char].adults;
sumChildren += array[char].children;
}
console.log(sumAdults + " " + sumChildren);
Less to remember if you can look for data in objects and data in an array of objects the same way. Enjoy
Array [
Object {
"product": Object {
"cat_id": "20",
"cat_name": "Pizza",
"detail": "Pizza sauce, Green pepper & mozarella cheese",
"discount_price": "",
"has_extra": "0",
"has_variation": "1",
"id": "46",
"image": "chicken-tikka-piza-recipe-main-photo.jpg",
"name": "Chicken Fajita",
"prep_time": "30",
"price": "310",
"status": "1",
"time_stamp": "2021-01-02 19:43:41",
"ven_id": "6",
},
"quantity": 1,
},
Object {
"product": Object {
"cat_id": "20",
"cat_name": "Pizza",
"detail": "Pizza Sauce, Tomato Green Paper, Olives Mashrooms And Chipotle Sauce with extra Creamy mayoneese",
"discount_price": "",
"has_extra": "0",
"has_variation": "0",
"id": "45",
"image": "chicken-tikka-piza-recipe-main-photo.jpg",
"name": "Chicken Tikka",
"prep_time": "15",
"price": "310",
"status": "1",
"time_stamp": "2021-01-02 19:41:56",
"ven_id": "6",
},
"quantity": 3,
},
]
How to calculate its total price where quantity is not the same also I want to calculate total price
I have this JSON file :
[{
"name": "bagette",
"price": "0.200"
}, {
"name": "farine",
"price": "1"
}, {
"name": "tomato",
"price": "1.200"
}, {
"name": "chocola",
"price": "4.000"
}]
I want to get the data from json file to an array in react for example :
console.log(data[0][0]); // bagette
console.log(data[0][1]); // 0.200
console.log(data[1][0]); // farine
console.log(data[3][1]); // 4.000
I'm a beginner in React Please can someone help me to write the code ?
var data = [{
"name": "bagette",
"price": "0.200"
}, {
"name": "farine",
"price": "1"
}, {
"name": "tomato",
"price": "1.200"
}, {
"name": "chocola",
"price": "4.000"
}];
data = data.map(val=>Object.values(val));
console.log(data[0][0]);
console.log(data[0][1]);
console.log(data[1][0]);
console.log(data[2][1]);
You can parse json into an object with JSON.parse(). Then you can map every object to an array. Note that this is the only way to totally ensure the order of the properties is the desired one as object properties have no guaranteed order.
const json = '[{"name": "bagette","price": "0.200"}, {"name": "farine","price": "1"},{"name":"tomato","price": "1.200"}, {"name": "chocola","price": "4.000"}]';
const data = JSON.parse(json);
const transformedData = data.map(obj => [obj.name, obj.price]);
console.log(transformedData[0][0]); // bagette
console.log(transformedData[0][1]); // 0.200
console.log(transformedData[1][0]); // farine
console.log(transformedData[3][1]); // 4.000
But I really don't know if that is a good idea. Why would you want to introduce magic numbers when you already have named properties to access in your dataset.
You can use Array.prototype.map() to return an array of array
var data = [{
"name": "bagette",
"price": "0.200"
}, {
"name": "farine",
"price": "1"
}, {
"name": "tomato",
"price": "1.200"
}, {
"name": "chocola",
"price": "4.000"
}];
data = data.map((x)=>[x.name , x.price]);
console.log(data[0][0]);
console.log(data[0][1]);
console.log(data[1][0]);
console.log(data[2][1]);
let arr = [{
"name": "bagette",
"price": "0.200"
}, {
"name": "farine",
"price": "1"
}, {
"name": "tomato",
"price": "1.200"
}, {
"name": "chocola",
"price": "4.000"
}]
let data = arr.map(item=>{
return [item.name, item.price]
})
That's you need?
Not exactly clear what you are trying to do
Yu can use
console.log(data[0].name) //bagette
console.log(data[0]['name']) //bagette
or to iterate through every property in object:
for(var propertyName in data[0]){
console.log(data[0][propertyName]);
}
I'm pretty new to Javascript, and I just learned about underscore.js. I have a deeply nested JSON object, and I need to use underscore to find key/value pairs, which I will then use to populate various HTML tables. If the structure was more shallow, using something like _.pluck would be easy, but I just don't know how to traverse past the first couple of nesting levels (i.e. surveyGDB, table, tablenames). The JSON object comes from an XML that is comprised of multiple nesting structures (mashed up from different database tables).
var JSONData =
"surveyGDB": {
"filename": "..\\Topo\\SurveyGeoDatabase.gdb",
"table": {
"tablename": [
{
"#text": "SurveyInfo\n ",
"record": {
"OBJECTID": "1",
"SiteID": "CBW05583-345970",
"Watershed": "John Day",
"VisitType": "Initial visit",
"SurveyInstrument": "Total Station",
"ImportDate": "2015-07-22T09:08:42",
"StreamName": "Duncan Creek",
"InstrumentModel": "TopCon Magnet v2.5.1",
"FieldSeason": "2015"
}
},
{
"#text": "QaQcPoints\n ",
"record": [
{
"OBJECTID": "1",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "tp",
"Count": "357"
},
{
"OBJECTID": "2",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "tb",
"Count": "92"
},
{
"OBJECTID": "3",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "to",
"Count": "8"
},
{
"OBJECTID": "4",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "bl",
"Count": "279"
},
{
"OBJECTID": "5",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "bf",
"Count": "18"
}
]
},
{
"#text": "QaQcPolygons\n ",
"record": [
{
"OBJECTID": "1",
"TIMESTAMP": "2015-07-22T09:43:08",
"SurveyExtentCount": "",
"WaterExtentCount": "",
"ChannelUnitsCount": "",
"ChannelUnitsUnique": ""
},
{
"OBJECTID": "2",
"TIMESTAMP": "2015-07-22T13:35:15",
"SurveyExtentCount": "1",
"WaterExtentCount": "1",
"ChannelUnitsCount": "21",
"ChannelUnitsUnique": "21"
}
]
}
]
}
}
}
For instance, I wanted all of the values for 'Code' in the 'QaQCPoints' table, so I tried:
var codes = _.flatten(_.pluck(JSONData.surveyGDB.table.tablename[1].record[0], "Code" ));
console.log(codes);
In the console, this returns an array with a length of 5, but with blank values.
What am I doing wrong?
I'd also rather search for the 'Code' values in the table based on something like the '#text' key value, instead of just using it's position in the object.
If I understood you correctly, you want to always search the record array within JSONData.surveyGDB.table.tablename array for some queries. This means you need to find the record based on some parameter and return something from the found record.
Do note that the record property is sometimes an array and sometimes an object (for table SurveyInfo) in your example so I'll assume you need to take this into account.
You can make a small function to extract data and handle both objects and arrays:
function extract(record, prop) {
if (Array.isArray(record)) {
return _.pluck(record, prop);
} else {
return record[prop];
}
}
Usage example:
I wanted all of the values for 'Code' in the 'QaQCPoints' table.
I'd also rather search for the 'Code' values in the table based on something like the '#text' key value, instead of just using it's position in the object.
To achieve this you first find a record using _.find, and then extract Code values from it using the method above:
var table = JSONData.surveyGDB.table.tablename;
// find an item that has `#text` property equal to `QaQcPoints`
var item = _.find(table, function(r) {
return r['#text'] === 'QaQcPoints';
});
// extract codes from the found item's record property
var code = extract(item.record, 'Code');
// output ["tp", "tb", "to", "bl", "bf"]
Running sample:
var JSONData = {
"surveyGDB": {
"filename": "..\\Topo\\SurveyGeoDatabase.gdb",
"table": {
"tablename": [{
"#text": "SurveyInfo",
"record": {
"OBJECTID": "1",
"SiteID": "CBW05583-345970",
"Watershed": "John Day",
"VisitType": "Initial visit",
"SurveyInstrument": "Total Station",
"ImportDate": "2015-07-22T09:08:42",
"StreamName": "Duncan Creek",
"InstrumentModel": "TopCon Magnet v2.5.1",
"FieldSeason": "2015"
}
}, {
"#text": "QaQcPoints",
"record": [{
"OBJECTID": "1",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "tp",
"Count": "357"
}, {
"OBJECTID": "2",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "tb",
"Count": "92"
}, {
"OBJECTID": "3",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "to",
"Count": "8"
}, {
"OBJECTID": "4",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "bl",
"Count": "279"
}, {
"OBJECTID": "5",
"TIMESTAMP": "2015-07-22T09:18:43",
"Code": "bf",
"Count": "18"
}]
}, {
"#text": "QaQcPolygons",
"record": [{
"OBJECTID": "1",
"TIMESTAMP": "2015-07-22T09:43:08",
"SurveyExtentCount": "",
"WaterExtentCount": "",
"ChannelUnitsCount": "",
"ChannelUnitsUnique": ""
}, {
"OBJECTID": "2",
"TIMESTAMP": "2015-07-22T13:35:15",
"SurveyExtentCount": "1",
"WaterExtentCount": "1",
"ChannelUnitsCount": "21",
"ChannelUnitsUnique": "21"
}]
}]
}
}
}
function extract(record, prop) {
if (Array.isArray(record)) {
return _.pluck(record, prop);
} else {
return record[prop];
}
}
var table = JSONData.surveyGDB.table.tablename;
var item = _.find(table, function(r) {
return r['#text'] === 'QaQcPoints';
});
console.dir(item);
var code = extract(item.record, 'Code');
console.log(code);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
You have a two stage problem. Stage one is figuring out which table is QaQcPoints. If that's always JSONData.surveyGDB.table.tablename[1], you're good.
The next stage is getting your data out. You can use native array manipulation most of the time (unless you're on really old browsers). So:
var table = JSONData.surveyGDB.table.tablename[1].record;
var codeArray = table.map(function(val) { return val.Code; });
Will do the trick.
[
{
"Hs": 5,
"Type": "Support",
"By": "William Shake the Speare",
"Product": "SA",
"Customer": "Huxley",
"State": "In progress"
},
{
"Hs": 2,
"Type": "Support",
"By": "Orwells",
"Product": "Web",
"Customer": "Infranet",
"State": "Closed"
}]
I have many of this objects and need to take Customer and sum (add) all of his "Hs" value. So the final array would be like:
[
{
"Customer" : "Infranet",
"Total_hs" : "18"
},
{
"Customer" : "Huxley",
"Total_hs" : "11"
}
]
I tried to find out with lodash and node foreach functions but can't, could you help me please? Thanks!
First, you must make sure you have the input data as a javascript object (so if it is a json string, it will need to be parsed with something like var items = JSON.parse(input_string))
var items = [{
"Hs": 5,
"Type": "Support",
"By": "William Shake the Speare",
"Product": "SA",
"Customer": "Huxley",
"State": "In progress"
}, {
"Hs": 2,
"Type": "Support",
"By": "Orwells",
"Product": "Web",
"Customer": "Infranet",
"State": "Closed"
}]
... next, create array of summed Hs values...
var totals = _.reduce(items, function(memo, item) {
// make sure that the customer is created on the output object, or initialise it to zero
memo[item.Customer] = memo[item.Customer] || 0;
// increment the Hs value with the current item's Hs value
memo[item.Customer] += item.Hs;
// return the current object for the next iteration of the loop
return memo;
// pass empty object to initialise reduce
}, {});
totals should now have array of objects, with customer name as key, and total Hs as value.
... next, reformat array to match desired data format...
var target_array = _.map(totals, function(item, key) {
return {
Customer: key,
Total_hs: item
}
});
... check the out put is correct...
console.log(target_array);
Given the JSON is provided as a JavaScript object (items in the sample below):
var items = [{
"Hs": 5,
"Type": "Support",
"By": "William Shake the Speare",
"Product": "SA",
"Customer": "Huxley",
"State": "In progress"
}, {
"Hs": 2,
"Type": "Support",
"By": "Orwells",
"Product": "Web",
"Customer": "Infranet",
"State": "Closed"
}];
var collected = _.map(_.keys(_.grouped(items, "Customer")), function(k) {
return {
Customer: k,
Hs: _.reduce(grouped[k], function(accumulator, v) {
return accumulator + v.Hs;
}, 0)
};
});