I have an empty Array
var kpi_arr = [];
I push in an Object
{
kpi_id: "18",
kpi_name: "CSAT",
kpi_target: "7",
kpi_months: [
{
monthname: "Aug 2017",
month_value: "",
month_id: "201",
month_kpi: "18"
},
{
monthname: "Sep 2017",
month_value: "",
month_id: "301",
month_kpi: "18"
}
]
}
The Array now has one object with the correct values. However when i push in a second object
{
kpi_id: "16",
kpi_name: "Updated handbooks",
kpi_target: " 100%",
kpi_months: [
{
monthname: "Aug 2017",
month_value: "",
month_id: "201",
month_kpi: "16"
},
{
monthname: "Sep 2017",
month_value: "",
month_id: "301",
month_kpi: "16"
}
]
}
the array content appears as below
[
{
kpi_id: "18",
kpi_name: "CSAT",
kpi_target: "7",
kpi_months: [
{
monthname: "Aug 2017",
month_value: "",
month_id: "201",
month_kpi: "16"
},
{
monthname: "Sep 2017",
month_value: "",
month_id: "301",
month_kpi: "16"
}
]
},
{
kpi_id: "16",
kpi_name: "Updated handbooks",
kpi_target: " 100%",
kpi_months: [
{
monthname: "Aug 2017",
month_value: "",
month_id: "201",
month_kpi: "16"
},
{
monthname: "Sep 2017",
month_value: "",
month_id: "301",
month_kpi: "16"
}
]
}
]
Note that all month_kpi property values change to 16.
What could be the reason for this?
I'm pushing these objects into the array via a for-loop in the format below.
var rolling_months_arr = get_rolling_13month_period();
var kpi_arr = [];
for(){
var month_kpi = getValueFromWherever();
var myobject = getObject(rolling_months_arr, month_kpi)
kpi_arr.push(myobject )
}
//------------------------------------------
function getObject(rolling_months_arr, month_kpi)
{
for ( var i = 0; i < rolling_months_arr.length; i++)
{
rolling_months_arr[i].month_kpi = month_kpi;
}
return rolling_months_arr;
}
function get_rolling_13month_period()
{
var month_arr = [];
for(){
month_arr.push({ "monthname": rolling_month, "month_id": month_id });
}
return month_arr;
}
rolling_months_arr is being reused, you need to create a new one inside the for()
you are using the same rolling_months_arr each time you add to the array.push, so the values are changed in all instances
put var rolling_months_arr = get_rolling_13month_period(); inside the for()
or clone that var:
for(){
var month_kpi = getValueFromWherever();
rolling_months_arr = _.cloneDeep(rolling_months_arr); //<= cloneDeep
var myobject = getObject(rolling_months_arr, month_kpi)
kpi_arr.push(myobject )
}
Related
I am trying to filter out all the data which has month 8
I have tried as following
https://jsfiddle.net/n2fypquz/2/
var json = [{
"empId": 175,
"Name": "Sai",
"Sal": 37000,
"doj": "2019-08-15 00:00:00"
},
{
"empId": 1751,
"Name": "Pavan",
"Sal": 57000,
"doj": "2019-07-15 00:00:00"
}
];
var month = '8';
let empData = json.filter(function(mgmtmrktshare) {
let date = mgmtmrktshare.doj;
let afterSplit = date.split("-");
return afterSplit[1] == month;
});
console.log(empData.length)
Whenever you are dealing with date values, its always better to use Date object.
Note: month in JS starts with 0 so Jan. is 0 and Sept. is 8
var json = [{
"empId": 175,
"Name": "Sai",
"Sal": 37000,
"doj": "2019-08-15 00:00:00"
},
{
"empId": 1751,
"Name": "Pavan",
"Sal": 57000,
"doj": "2019-07-15 00:00:00"
}
];
var month = '8';
let empData = json.filter(({
doj
}) => (new Date(doj)).getMonth() === month - 1);
console.log(empData.length)
You could slice the wanted part from the timestamp and get a numerical value, then compare with the month.
var json = [{ empId: 175, Name: "Sai", Sal: 37000, doj: "2019-08-15 00:00:00" }, { empId: 1751, Name: "Pavan", Sal: 57000, doj: "2019-07-15 00:00:00" }],
month = '8',
empData = json.filter(({ doj }) => +doj.slice(5, 7) == month);
console.log(empData);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Just use parseInt(afterSplit[1],'10')
var json = [{
"empId": 175,
"Name": "Sai",
"Sal": 37000,
"doj": "2019-08-15 00:00:00"
},
{
"empId": 1751,
"Name": "Pavan",
"Sal": 57000,
"doj": "2019-07-15 00:00:00"
}
];
var month = '8';
let empData = json.filter(function(mgmtmrktshare) {
let date = mgmtmrktshare.doj;
let afterSplit = date.split("-");
return parseInt(afterSplit[1], 10) == month;
});
console.log(empData.length)
You can use the below code.
var json = [{
"empId": 175,
"Name": "Sai",
"Sal": 37000,
"doj": "2019-08-15 00:00:00"
},
{
"empId": 1751,
"Name": "Pavan",
"Sal": 57000,
"doj": "2019-07-15 00:00:00"
}
];
var month = '8'
function search(month, json){
for (var i=0; i < json.length; i++) {
if ((json[i].doj).getMonth() === month - 1) {
return json[i];
console.log(json[i])
}
}
}
I have an JSON like this
"result": [{
"channel": "A",
"mkp": "ABC",
"qtd": 6,
"total": 2938.2,
"data": "2019-02-16",
"time": "22:30:40"
}, {
"channel": "C",
"mkp": "DEF",
"qtd": 1545,
"total": 2127229.64,
"data": "2019-02-20",
"time": "17:19:49"
}, {
"channel": "C",
"mkp": "JKL",
"qtd": 976,
"total": 1307328.37,
"data": "2019-02-20",
"time": "17:19:53"
}, {
"channel": "U",
"mkp": "PQR",
"qtd": 77,
"total": 98789.87,
"data": "2019-02-20",
"time": "16:12:31"
}, {
"channel": "U",
"mkp": "STU",
"qtd": 427,
"total": 433206.62,
"data": "2019-02-20",
"time": "17:04:27"
}
]
I need to sum the QTD, the total and return the newest data + time when the channel is the same (eg.: Channel C and U have 2 entries), if it's not so I only will display the values, but I can't figure it out how could I iterate and do these math. Someone could help?
A sample of what I want:
"A": [{
"qtd": 6,
"total": 2938.20,
"dateTime": 2019 - 02 - 16 22: 30: 40 "
}],
"C": [{
"qtd": 2.521,
"total": 3434558.01,
"dateTime": 2019 - 02 - 20 17: 19: 53 "
}],
"U": [{
"qtd": 504,
"total": 531996,
49,
"dateTime": 2019 - 02 - 20 17: 04: 27 "
}]
Currently I separated the values using filter like this:
this.channelA = this.receivedJson.filter(({ channel }) => channel === "A");
You could use reduce method and return one object with object as values.
const data = [{"channel":"A","mkp":"ABC","qtd":6,"total":2938.2,"data":"2019-02-16","time":"22:30:40"},{"channel":"C","mkp":"DEF","qtd":1545,"total":2127229.64,"data":"2019-02-20","time":"17:19:49"},{"channel":"C","mkp":"JKL","qtd":976,"total":1307328.37,"data":"2019-02-20","time":"17:19:53"},{"channel":"U","mkp":"PQR","qtd":77,"total":98789.87,"data":"2019-02-20","time":"16:12:31"},{"channel":"U","mkp":"STU","qtd":427,"total":433206.62,"data":"2019-02-20","time":"17:04:27"}]
const res = data.reduce((r, {channel, qtd, total, data, time}) => {
const dateTime = `${data} ${time}`
if(!r[channel]) r[channel] = {qtd, total, dateTime}
else {
r[channel].total += total
r[channel].qtd += qtd;
r[channel].dateTime = dateTime
}
return r;
}, {})
console.log(res)
You an use reduce to group the values based on channel like this:
const input = [{"channel":"A","mkp":"ABC","qtd":6,"total":2938.2,"data":"2019-02-16","time":"22:30:40"},{"channel":"C","mkp":"DEF","qtd":1545,"total":2127229.64,"data":"2019-02-20","time":"17:19:49"},{"channel":"C","mkp":"JKL","qtd":976,"total":1307328.37,"data":"2019-02-20","time":"17:19:53"},{"channel":"U","mkp":"PQR","qtd":77,"total":98789.87,"data":"2019-02-20","time":"16:12:31"},{"channel":"U","mkp":"STU","qtd":427,"total":433206.62,"data":"2019-02-20","time":"17:04:27"}]
const merged = input.reduce((acc, { channel, qtd, total, data, time }) => {
acc[channel] = acc[channel] || [{ qtd: 0, total: 0, dateTime:'' }];
const group = acc[channel][0];
group.qtd += qtd;
group.total += total;
const dateTime = `${data} ${time}`
if(dateTime > group.dateTime)
group.dateTime = dateTime;
return acc;
}, {})
console.log(merged)
I need to get an array to display the following way:
var categories = data.categories;
for(a = 0; a < data.count; a++){
var names[a] = data.name[a];
var values[a] = data.value[a];
}
There will be one "Categories" array, for example "May 2017","June 2017","July 2017","August 2017","September 2017". Then there may be 3/4 arrays containing "Name" and "Values" and I want these to loop through a loop to loop and assign these to variables.
At the moment I'm getting 'Undefined" in all variables.
My output in php is currently:
[
[{
"name": "Leads",
"data": [524, 419, 502, 598, 873],
"color": "#00afef"
}, {
"name": "Purchases",
"data": [37, 18, 32, 36, 44],
"color": "#f00"
}], {
"categories": ["May 2017", "June 2017", "July 2017", "August 2017", "September 2017"]
}
]
The JSON you're getting back has horrible schema and you're doing weird things in JS. Hopefully the following will get you back on track:
var data = [
[{
"name": "Leads",
"data": [524, 419, 502, 598, 873],
"color": "#00afef"
}, {
"name": "Purchases",
"data": [37, 18, 32, 36, 44],
"color": "#f00"
}], {
"categories": ["May 2017", "June 2017", "July 2017", "August 2017", "September 2017"]
}
];
var categories = data[1].categories;
var names = [];
var values = [];
for (var a = 0; a < data[0].length; a++) {
names.push(data[0][a].name);
values.push(data[0][a]);
}
console.log(categories);
console.log(names);
console.log(values);
I have bunch of objects in an array with format as below:
[
{
"card_details": {
"cardType": "gift"
},
"merchant_details": {
"merchantName": "Walter Heisenberg1",
"timeZone": "+05:30",
}
},
{
"card_details": {
"cardType": "coupon",
"cardTitle": "Coupon",
"messageUser": "Hi",
"punchCount": null,
"messageReachPunchLimit": "Get your Freebie!",
"merchantId": "59c214000e1a7825184cb813",
"expiryDate": "21 Sep 2019",
"discountPercent": "15",
"cardImageUrl": ""
},
"merchant_details": {
"merchantName": "Walter Heisenberg1",
"timeZone": "+05:30"
}
},
{
"card_details": {
"cardType": "punch",
"cardTitle": "BlueM2",
"messageUser": "Welcome!",
"expiryDate": "21 Sep 2019",
"punchCount": 25,
"messageReachPunchLimit": "Get your Freebie!",
"merchantId": "59c214000e1a7825184cb813",
"cardImageUrl": "http://139.59.179.111/cloopapi/undefined"
},
"merchant_details": {
"merchantName": "Walter Heisenberg1",
"timeZone": "+05:30"
}
}
]
I want to filter the objects based on cardType in the card_details object. But I want to search from the array. For example, if I search for ["coupon","gift"], then I should get the all the cards which have the card_details.cardType as coupon or gift.
I need to be able to do this in Node.js,i.e., Javascript.
You can use ES6 filter and includes functions here:
collection.filter(item => ['coupon', 'gift'].includes(item.card_details.cardType))
I have following input json payload,
{
"Products": {
"Product": [
{
"ProductID": 458761,
"Designation": "CB 024-2001",
"EntryDate": "2002-01-20T19:00:00.000-05:00",
"S1": "024",
"S2": 2001,
"Year": 2001
},
{
"ProductID": 458234,
"Designation": "AGRS03/08",
"EntryDate": "2008-03-05T19:00:00.000-05:00",
"S1": "03",
"S2": "08",
"Year": 2008
}
]
}
}
And now I need to transform it into the following JSON format.
[
{
"Designation": "CB 024-2001",
"EntryDate": "2002-01-20T19:00:00.000-05:00",
"ProductID": 458761,
"S1": "024",
"S2": 2001,
"Year": 2001
},
{
"Designation": "AGRS03/08",
"EntryDate": "2008-03-05T19:00:00.000-05:00",
"ProductID": 458761,
"S1": "03",
"S2": "08",
"Year": 2008
}
]
Can someone please help me to write a JavaScript to achieve this task. Any help is really appreciated.
EDIT: You changed the question :(
Assuming your original json is stored in a variable named input. This can be done using this code:
var output = input.Products.Product;
ORIGINAL: You can do this using map:
var output = input.Products.Product.map(function(inObj) {
return {
"Designation": inObj.Designation,
"EntryDate": inObj.EntryDate,
"S1": inObj.S1,
"S2": inObj.S2,
"Year": inObj.Year
}
});
This will give you the output you want - an array of objects, with the ProductIDs removed. I'm a bit rusty when it comes to working with object references, but you could possibly shorten this using delete:
var output = input.Products.Product.map(function(inObj) {
var outObj = inObj;
delete outObj.ProductID;
return outObj;
});
This will change the original input values as well though, so i wouldn't recommend it unless you don't plan on using that data again.
var first = {
"Products": {
"Product": [
{
"ProductID": 458761,
"Designation": "CB 024-2001",
"EntryDate": "2002-01-20T19:00:00.000-05:00",
"S1": "024",
"S2": 2001,
"Year": 2001
},
{
"ProductID": 458234,
"Designation": "AGRS03/08",
"EntryDate": "2008-03-05T19:00:00.000-05:00",
"S1": "03",
"S2": "08",
"Year": 2008
}
]
}
}
then:
var second = first.Products.Product;
To make it exactly like you want:
for(var i = 0; i<second.length; i++){
delete second[i].ProductID;
}
You can use this little function:
var a = {
"Products": {
"Product": [{
"ProductID": 458761,
"Designation": "CB 024-2001",
"EntryDate": "2002-01-20T19:00:00.000-05:00",
"S1": "024",
"S2": 2001,
"Year": 2001
}, {
"ProductID": 458234,
"Designation": "AGRS03/08",
"EntryDate": "2008-03-05T19:00:00.000-05:00",
"S1": "03",
"S2": "08",
"Year": 2008
}]
}
};
function newJSON(array){
var b = array.Products.Product;
b.forEach(function(e){delete e.ProductID});
return JSON.stringify(b);
}
document.write(newJSON(a));
With underscore:
var result = _.map(data.Products.Products, (product) => {
return _.omit(product, 'ProductID');
});