JS\Node Reduce group by multiple properties and summarize JSON data - javascript

I have following data that i'm trying to group summarize and add back to related object.
{
"Schedule": [
{
"AppointmentId": 123456,
"Start": "2021-11-02T09:00:00.000Z",
"Location": 87925,
"InsuranceName": "Geico",
"InsuranceFinancialClass": "Commercial",
"AppointmentName": "Online",
"LocalDate": "11/02/2021",
"LocalTime": "9:00 AM",
"TimeOfDay": "AM"
},
{
"AppointmentId": 123457,
"Start": "2021-11-03T10:00:00.000Z",
"Location": 87925,
"InsuranceName": "Farmers",
"InsuranceFinancialClass": "Commercial",
"AppointmentName": "InPerson",
"LocalDate": "11/03/2021",
"LocalTime": "10:00 AM",
"TimeOfDay": "AM"
},
{
"AppointmentId": 123458,
"Start": "2021-11-03T11:00:00.000Z",
"Location": 87925,
"InsuranceName": "Farmers",
"InsuranceFinancialClass": "Commercial",
"AppointmentName": "InPerson",
"LocalDate": "11/03/2021",
"LocalTime": "11:00 AM",
"TimeOfDay": "AM"
}
]
}
I would like the final result to look like the following:
{
"Schedule": [
{
"AppointmentId": 123456,
"Start": "2021-11-02T09:00:00.000Z",
"Location": 87925,
"InsuranceName": "Geico",
"InsuranceFinancialClass": "Commercial",
"AppointmentName": "Online",
"LocalDate": "11/02/2021",
"LocalTime": "9:00 AM",
"TimeOfDay": "AM",
"Summary": {
"InsuranceName": {
"Total": 1,
"Geico": 1
},
"InsuranceFinancialClass": {
"Total": 1,
"Commercial": 1
},
"AppointmentName": {
"Total": 1,
"Online": 1,
"InPerson": 1
},
"LocalDate": {
"Total": 1,
"11/02/2021": 1,
}
}
},
{
"AppointmentId": 123457,
"Start": "2021-11-03T10:00:00.000Z",
"Location": 87925,
"InsuranceName": "Farmers",
"InsuranceFinancialClass": "Commercial",
"AppointmentName": "InPerson",
"LocalDate": "11/03/2021",
"LocalTime": "10:00 AM",
"TimeOfDay": "AM",
"Summary": {
"InsuranceName": {
"Total": 2,
"Farmers": 2
},
"InsuranceFinancialClass": {
"Total": 2,
"Commercial": 2
},
"AppointmentName": {
"Total": 2,
"InPerson": 2
},
"LocalDate": {
"Total": 2,
"11/03/2021": 2
}
}
}
{
"AppointmentId": 123458,
"Start": "2021-11-03T11:00:00.000Z",
"Location": 87925,
"InsuranceName": "Farmers",
"InsuranceFinancialClass": "Commercial",
"AppointmentName": "InPerson",
"LocalDate": "11/03/2021",
"LocalTime": "11:00 AM",
"TimeOfDay": "AM",
"Summary": {
"InsuranceName": {
"Total": 2,
"Farmers": 2
},
"InsuranceFinancialClass": {
"Total": 2,
"Commercial": 2
},
"AppointmentName": {
"Total": 2,
"InPerson": 2
},
"LocalDate": {
"Total": 2,
"11/03/2021": 2
}
}
}
]
}
I have following JS code that:
const props = ['LocalDate', 'AppointmentName', 'InsuranceFinancialClass', 'InsuranceName'];
const result = JSONData.reduce((r, e) => {
Object.entries(e).forEach(([k, v]) => {
if (props.includes(k)) {
if (!r[k]) r[k] = {};
r[k].Total = (r[k].Total || 0) + 1;
r[k][v] = (r[k][v] || 0) + 1;
}
});
return r;
}, {});
{
"Schedule": {
"InsuranceName": {
"Total": 3,
"Geico": 1,
"Farmers": 2
},
"InsuranceFinancialClass": {
"Total": 3,
"Commercial": 3
},
"AppointmentName": {
"Total": 3,
"Online": 1,
"InPerson": 2
},
"LocalDate": {
"Total": 3,
"11/02/2021": 1,
"11/03/2021": 2
}
}
}
How should the code be written produce desired result?
The summary object should be aggregate by date and appended to original object that matches the date.
Thanks in advance for your help and suggestions.

I was able to work out a solution but not sure if it's efficient.
const props = ['LocalDate', 'AppointmentName', 'InsuranceFinancialClass', 'InsuranceName'];
const groupedByDateSummary = filteredByPatientAppointment.reduce((r, e) => {
if (!r[e.LocalDate]) r[e.LocalDate] = {};
Object.entries(e).forEach(([k, v]) => {
if (props.includes(k)) {
if (!r[e.LocalDate][k]) r[e.LocalDate][k] = {};
r[e.LocalDate][k].Total = (r[e.LocalDate][k].Total || 0) + 1;
r[e.LocalDate][k][v] = (r[e.LocalDate][k][v] || 0) + 1;
}
});
return r;
}, {});
const slotObjWithSummary = _.each(filteredByPatientAppointment, (summary) => {
for (const [key, value] of Object.entries(groupedByDateSummary)) {
if (key === summary.LocalDate) {
summary.Summary = value;
}
}
return summary;
});

Related

How should I slice the array of objects of objects

i have a array which looks like this :
[
{
"1": {
"id": 1,
"price": 569.2,
"marketcap": 94943213384.68,
"timestamp": 1637136047,
"datetime": "2021-11-17 08:00:47"
},
"2": {
"id": 2,
"price": 59745.7,
"marketcap": 1127634159487.02,
"timestamp": 1637136064,
"datetime": "2021-11-17 08:01:04"
},
"3": {
"id": 3,
"price": 592.53,
"marketcap": 11199494644.95126,
"timestamp": 1637136053,
"datetime": "2021-11-17 08:00:53"
}
},
{
"1": {
"id": 1,
"last30dcommits": null,
"activity": "No Github Repo Found"
},
"2": {
"id": 2,
"last30dcommits": 109,
"activity": "High Activity"
},
"3": {
"id": 3,
"last30dcommits": 44,
"activity": "Medium Activity"
}
},
{
"1": {
"id": 1,
"change24h": -6.75,
"change7d": -10.18,
"change30d": 17.32,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
},
"2": {
"id": 2,
"change24h": -3.22,
"change7d": -12.58,
"change30d": -5.17,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
},
"3": {
"id": 3,
"change24h": -6.37,
"change7d": -17.81,
"change30d": -5.04,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
}
},
{
"1": {
"tweetsRating": 44.04,
"newsRating": 38.42
},
"2": {
"tweetsRating": 14.76,
"newsRating": -1.27
},
"3": {
"tweetsRating": 0,
"newsRating": 44.35
}
},
]
I am implementing a pagination in the front end in which I am slicing each objects in each object in the array to a given value so that I get a result something like this if I slice (0,2)
[
{
"1": {
"id": 1,
"price": 569.2,
"marketcap": 94943213384.68,
"timestamp": 1637136047,
"datetime": "2021-11-17 08:00:47"
},
"2": {
"id": 2,
"price": 59745.7,
"marketcap": 1127634159487.02,
"timestamp": 1637136064,
"datetime": "2021-11-17 08:01:04"
}
},
{
"1": {
"id": 1,
"last30dcommits": null,
"activity": "No Github Repo Found"
},
"2": {
"id": 2,
"last30dcommits": 109,
"activity": "High Activity"
}
},
{
"1": {
"id": 1,
"change24h": -6.75,
"change7d": -10.18,
"change30d": 17.32,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
},
"2": {
"id": 2,
"change24h": -3.22,
"change7d": -12.58,
"change30d": -5.17,
"timestamp24h": 1637035200,
"timestamp7d": 1636516800,
"timestamp30d": 1634529600
}
},
{
"1": {
"tweetsRating": 44.04,
"newsRating": 38.42
},
"2": {
"tweetsRating": 14.76,
"newsRating": -1.27
}
},
]
means only 2 objects should be sliced out.
my function is as following but its not working.
const sliceData = (array,start,end)=>{
array.forEach(arr=>{
Object.keys(arr).slice(start - 1, end + 1).reduce((result, key) => {
result[key] = arr[key];
return result;
}
})
}
Here start and end represents the index of starting object in a page and index of last object in the page respectively.
Can anyone help me with the code? I'll be very grateful.
You could slice the entries for new objects.
const
data = [{ "1": { id: 1, price: 569.2, marketcap: 94943213384.68, timestamp: 1637136047, datetime: "2021-11-17 08:00:47" }, "2": { id: 2, price: 59745.7, marketcap: 1127634159487.02, timestamp: 1637136064, datetime: "2021-11-17 08:01:04" }, "3": { id: 3, price: 592.53, marketcap: 11199494644.95126, timestamp: 1637136053, datetime: "2021-11-17 08:00:53" } }, { "1": { id: 1, last30dcommits: null, activity: "No Github Repo Found" }, "2": { id: 2, last30dcommits: 109, activity: "High Activity" }, "3": { id: 3, last30dcommits: 44, activity: "Medium Activity" } }, { "1": { id: 1, change24h: -6.75, change7d: -10.18, change30d: 17.32, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 }, "2": { id: 2, change24h: -3.22, change7d: -12.58, change30d: -5.17, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 }, "3": { id: 3, change24h: -6.37, change7d: -17.81, change30d: -5.04, timestamp24h: 1637035200, timestamp7d: 1636516800, timestamp30d: 1634529600 } }, { "1": { tweetsRating: 44.04, newsRating: 38.42 }, "2": { tweetsRating: 14.76, newsRating: -1.27 }, "3": { tweetsRating: 0, newsRating: 44.35 } }],
result = data.map(o => Object.fromEntries(Object.entries(o).slice(0, 2)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I really think the data format/structure sent from BackEnd should be changed. Instead of sending Array of objects you should consider sending Array of Array of objects from Backend. This is because essentially the id of the object is key. However, Here is how you can slice the data if you do not have permission to change data sent from Backend.
const sliceData = (array,start,end)=>{
return array.map(el => {
let newObj = Object.values(el).filter(x => x.id>start && x.id<=end).reduce((acc,x) => {
acc[x.id] = x;
return acc;
} ,{})
return newObj;
});
}

How can i make json file like this when i input

How can i make the cart_items like my expetation.. just it no more.. my problem just it :D
i just wanna make my cart_items like this.. hope you are can help me thanks. did I make the wrong method? and one more thing, i wanna make the qty inside the cart_items
this is my expectation
"cart": [
{
"id": 1,
"date": "12/10/2020",
"store": {
"id": 1,
"name": "Dirumah Aja",
"promo": 1
},
"cart_items": [
{
"id": 1,
"product": {
"id": 1,
"name": "Bakso Urat",
"price": 10000,
"promo": {
"nama": "promo"
}
},
"qty": 5
}
]
}
]
and this is what I got
"cart": [
{
"cart_items": {
"name": "Steak Sapi Impor",
"price": "38000",
"stock": "4",
"image": "https://firebasestorage.googleapis.com/v0/b/francise-fb70a.appspot.com/o/steak.jpg?alt=media&token=46e0d769-96d3-440f-8edb-5fce2481ace0",
"promo": 3,
"id": 8,
"qty": 1
},
"store": {
"name": "Amanda Foods Store",
"email": "amanda#food.com",
"store_image": "https://firebasestorage.googleapis.com/v0/b/francise-fb70a.appspot.com/o/full_hd_retina.jpeg?alt=media&token=3e602e86-661b-48ee-9e9c-af9f94a170d1",
"product": [
5,
7,
8,
2
],
"store_promo": 1,
"location": {
"street_name": "Jl. Kebon Gedang II B",
"province": "Jawa Barat",
"city": "Bandung",
"post_code": "40285"
},
"id": 1
},
"date_order": "Nov 03 2020 08:48:03",
"id": 2
}
]
This is my data
data() {
return {
promo_id: [],
promo_partner: [],
products: {},
qty: 1,
cart_items: [
{}
]
};
and this is my method
addToCart() {
const date = (new Date()).toString().split(' ').splice(1,4).join(' ')
this.products.cart_items = this.product;
this.products.cart_items.qty = this.qty;
this.products.store = this.partner;
this.products.date_order = date;
console.log(this.cart_items)
axios
.post("http://localhost:3000/cart/", this.products)
.then(() => {
swal("Belanja Berhasil!", {
icon: "success",
});
})
.catch((error) => console.log(error));
}
}
You need to use .push() to add items to an array. You're replacing the array with this.product.
if (!this.products.cart_items) { // initialize cart_items if necessary
this.products.cart_items = [];
}
this.products.cart_items.push({id: this.product.id, product: this.product, qty: this.qty});

Keys problems when mapping using Lodash

I am trying to get my monthly sales and put them on the chart. They're going to be grouped by year. This is what I've done so far:
let result = _
.chain(res.data)
.groupBy("_id.year")
.value();
result = _.map(result, (yearlyValues, key) => {
return {
[key]: _.map(yearlyValues, yearlyValuesItems => {
return {
[yearlyValuesItems._id.month]: yearlyValuesItems.count
};
})
};
});
result = _.map(result, (value, key) => {
return _.map(value, (innerValues, innerKey) => {
return innerValues;
})
})
result = _.flatten(result);
result = _.map(result, (value, key) => {
return _.map(value, (items, keys) => {
return _.map(items, (a, b) => {
return a
})
})
});
for (var i in result) {
final_count.push(_.flatten(result[i]));
}
console.log(final_count);
$scope.labels_monthly = monthNames;
$scope.series = Object.keys(final_count);
$scope.data = Object.values(final_count);
$scope.options = {
legend: {
display: true
}
};
My data: https://pastebin.com/WhJ0atTX
My output: https://jsfiddle.net/asfxtor4/1/
My problem is that I am getting keys 0, 1, 2 instead of 2016, 2017, 2018.
And then inside I need to get key as a month number.This key has to be 2 because the month is March, but it generates 0. You can see in the pictures.
This is my desired output: https://pastebin.com/iChhrnNG, I need to get an array of all months and if there is data for just March lets say, all other array elements should be 0.
You can get an array of objects with years and values by adding 'entries' e.g.
let result = _
.chain(data)
.groupBy("_id.year")
.entries()
.value()
.map((item) => ({year: item[0], values: item[1]}));
You then won't need:
result = _.map(result, (yearlyValues, key) => {
return {
[key]: _.map(yearlyValues, yearlyValuesItems => {
return {
[yearlyValuesItems._id.month]: yearlyValuesItems.count
};
})
};
});
You can see a fiddle here:
https://jsfiddle.net/Fresh/yr8bqteL/
Hopefully this info will help you complete this assignment.
Since you know you always have 12 months you could be somewhat less fancy and do this:
data = [{
"_id": {
"month": 12,
"year": 2018
},
"count": 3
},
{
"_id": {
"month": 11,
"year": 2018
},
"count": 4
},
{
"_id": {
"month": 9,
"year": 2018
},
"count": 3
},
{
"_id": {
"month": 8,
"year": 2018
},
"count": 4
},
{
"_id": {
"month": 5,
"year": 2018
},
"count": 4
},
{
"_id": {
"month": 7,
"year": 2018
},
"count": 2
},
{
"_id": {
"month": 4,
"year": 2018
},
"count": 5
},
{
"_id": {
"month": 9,
"year": 2017
},
"count": 2
},
{
"_id": {
"month": 8,
"year": 2017
},
"count": 4
},
{
"_id": {
"month": 11,
"year": 2017
},
"count": 4
},
{
"_id": {
"month": 10,
"year": 2017
},
"count": 3
},
{
"_id": {
"month": 4,
"year": 2017
},
"count": 2
},
{
"_id": {
"month": 2,
"year": 2017
},
"count": 3
},
{
"_id": {
"month": 6,
"year": 2017
},
"count": 1
},
{
"_id": {
"month": 1,
"year": 2018
},
"count": 4
},
{
"_id": {
"month": 2,
"year": 2018
},
"count": 4
},
{
"_id": {
"month": 3,
"year": 2018
},
"count": 9
},
{
"_id": {
"month": 10,
"year": 2018
},
"count": 3
},
{
"_id": {
"month": 3,
"year": 2017
},
"count": 9
},
{
"_id": {
"month": 7,
"year": 2017
},
"count": 2
},
{
"_id": {
"month": 12,
"year": 2017
},
"count": 3
},
{
"_id": {
"month": 3,
"year": 2016
},
"count": 1
},
{
"_id": {
"month": 1,
"year": 2017
},
"count": 3
},
{
"_id": {
"month": 6,
"year": 2018
},
"count": 3
}
]
let result = _
.chain(data)
.groupBy("_id.year")
.reduce((obj, current, index) => {
var sortedMonths = _.sortBy(current, (v) => v._id.month), monthsArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
_.map(current, (curObj, i) => monthsArray[_.get(sortedMonths, '[' + i + ']._id.month') - 1] = sortedMonths[i].count)
return _.extend(obj, { [index]: monthsArray })
}, {})
.value();
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Fuzzy search deep array only on certain properties

I have a JSON dataset which could be very large when it returns, with the following structure for each object:
{
"ctr": 57,
"averageECPC": 23,
"cost": 2732.54,
"margin": 66,
"profit": 2495.9,
"property": {
"value": "Izzby",
"uri": "/Terrago/2"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 2573.13,
"compare": 0
},
"children": [{
"ctr": 79,
"averageECPC": 54,
"cost": 3554.78,
"margin": 88,
"profit": 3145.81,
"property": {
"value": "Comvex",
"uri": "/Octocore/4"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 1247.92,
"compare": 0
}
}]
}
Now I want to search all objects in the array and return only objects which include a string of some sort, but I only want to search certain properties.
I basically have another array which contains the keys I want to search, e.g.
const iteratees = ['ctr', 'property.value', 'status.stage']
I have lodash available within the project, but I have no idea where to start.
Any ideas?
You could use filter(), some() and reduce() to do this.
const iteratees = ['ctr', 'property.value', 'status.stage'];
var searchFor = 'lo';
var result = arr.filter(function(o) {
return iteratees.some(function(e) {
var res = e.split('.').reduce(function(a, b) {
if(a) return a[b];
}, o);
if(res) {
if((res).toString().indexOf(searchFor) != -1) return true;
}
})
})
var arr = [{
"ctr": 'lorem',
"averageECPC": 23,
"cost": 2732.54,
"margin": 66,
"profit": 2495.9,
"property": {
"value": "Izzby",
"uri": "/Terrago/2"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 2573.13,
"compare": 0
},
"children": [{
"ctr": 79,
"averageECPC": 54,
"cost": 3554.78,
"margin": 88,
"profit": 3145.81,
"property": {
"value": "Comvex",
"uri": "/Octocore/4"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 1247.92,
"compare": 0
}
}]
}, {
name: 'lorem',
ctr: 12,
property: {
value: 1
},
status: {
stage: 1
}
}, {
name: 'ipsum'
}]
const iteratees = ['ctr', 'property.value', 'status.stage'];
var searchFor = 'lo';
var result = arr.filter(function(o) {
return iteratees.some(function(e) {
var res = e.split('.').reduce(function(a, b) {
if (a) return a[b];
}, o);
if (res) {
if ((res).toString().indexOf(searchFor) != -1) return true;
}
})
})
console.log(result)

How to remove parent fields in json variable and convert it to json in javascript or jquery

Am working on some project which has lot of objects involvement. I have below requirement in javascript or jQuery.
I have below object:
var dataset = {
"d0": { "id": 0, "name": "Housing", "value": 18 },
"d1": { "id": 1, "name": "Travel", "value": 31.08 },
"d2": { "id": 2, "name": "Restaurant", "value": 64 },
"d3": { "id": 3, "name": "Bank", "value": 3 },
"d4": { "id": 4, "name": "Movies", "value": 10 }
};
How can I remove parent fields and make it as object like below ?
var d= [
{ "id": 0, "name": "Housing", "value": 18 },
{ "id": 1, "name": "Travel", "value": 31.08 },
{ "id": 2, "name": "Restaurant", "value": 64 },
{ "id": 3, "name": "Bank", "value": 3 },
{ "id": 4, "name": "Movies", "value": 10 }
]
After doing this, I wanted to pass this object to a javascript function which manipulates the value of this variable d.
if(value >= 10 && value <= 20) {
d[index].value = 7;
}
if(value >= 20 && value <= 40) {
d[index].value = 8;
}
Updated object should look like :
var d= [
{ "id": 0, "name": "Housing", "value": 7 },
{ "id": 1, "name": "Travel", "value": 8 },
{ "id": 2, "name": "Restaurant", "value": 64 },
{ "id": 3, "name": "Bank", "value": 3 },
{ "id": 4, "name": "Movies", "value": 10 }
]
I tried looping to the dataset using for loop, but couldn't achieve my requirement.
Any help?
Use Array#forEach with Object.keys() to iterate over object.
var dataset = {
"d0": { "id": 0, "name": "Housing", "value": 18 },
"d1": { "id": 1, "name": "Travel", "value": 31.08 },
"d2": { "id": 2, "name": "Restaurant", "value": 64 },
"d3": { "id": 3, "name": "Bank", "value": 3 },
"d4": { "id": 4, "name": "Movies", "value": 10 }
};
// Declare resulting empty array
var d = [];
// Get object keys and iterate over them
Object.keys(dataset).forEach(function (key) {
// Get the value from the object
var value = dataset[key].value;
// Update values if in the range
if(value >= 10 && value <= 20) {
dataset[key].value = 7;
} else if(value > 20 && value <= 40) {
dataset[key].value = 8;
}
// Push the updated(or not) value in the array
d.push(dataset[key]);
});
console.log(d);
document.getElementById('result').innerHTML = JSON.stringify(d, null, 4);
<pre id="result"></pre>
Just map it with Array.prototype.map method:
var dataset = {
"d0": { "id": 0, "name": "Housing", "value": 18 },
"d1": { "id": 1, "name": "Travel", "value": 31.08 },
"d2": { "id": 2, "name": "Restaurant", "value": 64 },
"d3": { "id": 3, "name": "Bank", "value": 3 },
"d4": { "id": 4, "name": "Movies", "value": 10 }
};
var d = Object.keys(dataset).map(function(key) {
var obj = dataset[key];
if (obj.value >= 10 && obj.value <= 20) {
obj.value = 7;
}
else if (obj.value >= 20 && obj.value <= 40) {
obj.value = 8;
}
return obj;
});
document.write('<pre>' + JSON.stringify(d, null, 4) + '</pre>');
You can do it this way:
var dataset = {
"d0": { "id": 0, "name": "Housing", "value": 18 },
"d1": { "id": 1, "name": "Travel", "value": 31.08 },
"d2": { "id": 2, "name": "Restaurant", "value": 64 },
"d3": { "id": 3, "name": "Bank", "value": 3 },
"d4": { "id": 4, "name": "Movies", "value": 10 }
};
First remove the string keys:
var d = [];
for(var k in dataset){
d.push(dataset[k]);
}
Then iterate over resulting array modifying it accordingly:
for(var k in d){
var value = d[k]['value'];
if(value>=10 && value<=20){
d[k].value=7;
}
if(value>=20 && value<=40){
d[k].value=8;
}
}
This gives the resuls you're looking for:
var dataset = {
"d0": { "id": 0, "name": "Housing", "value": 18 },
"d1": { "id": 1, "name": "Travel", "value": 31.08 },
"d2": { "id": 2, "name": "Restaurant", "value": 64 },
"d3": { "id": 3, "name": "Bank", "value": 3 },
"d4": { "id": 4, "name": "Movies", "value": 10 }
};
var d = Object.keys(dataset).map(function(key) {
var v = dataset[key].value;
if (v >= 10 && v <= 20) dataset[key].value = 7;
else if (v > 20 && v <= 40) dataset[key].value = 8;
return dataset[key];
});
try this,
function datasetToArrOfObj(dataset){
var rslt = [];
for (var key in dataset) {
if (dataset.hasOwnProperty(key)) {
rslt.push(dataset[key]);
}
}
return rslt;
}

Categories

Resources