Reassigning from JS Object A property value to another - javascript

I'm trying to assign prices to my items from a JSON A to JSON B, managed to get the prices and reassign it to the property but not to the whole object.
here's a snippet of my code, which gets the prices from the first Object and reassigning it to TotalOrignialValue however how can I push it back to the newJson object?
Is there a more pleasing way of achieving this?
// Code goes here
var items = {
"TransactionLine": [
{
"Product": {
"Id": null,
"Codes": [
"1112"
],
"Sku": null
},
"TotalValue": 2.35,
},
{
"Product": {
"Id": null,
"Codes": [
"1113"
],
"Sku": null
},
"TotalValue": 2.15,
}
],
"CustomData": {}
};
var itemPrice = [];
for (var i = 0; i < items.TransactionLine.length; i++) {
var el = items.TransactionLine[i];
itemPrice.push(el.TotalValue);
console.log(el.TotalValue);
}
var newJson = {
"OrderLines": [
{
"Product": {
"Id": 9,
"Codes": [
"1113"
],
"Sku": "CS1113"
},
"TotalOriginalValue": 0, // asign the price here
},
{
"Product": {
"Id": 21,
"Codes": [
"1112"
],
"Sku": "CS1112"
},
"TotalOriginalValue": 0, // asign the price here
}
]
};
var newPrice = [];
for (var x = 0; x < newJson.OrderLines.length; x++) {
var xd = newJson.OrderLines[x].TotalOriginalValue;
xd = itemPrice[x];
newjson = {
"TotalOriginalValue": xd
};
newPrice.push(newjson);
}
console.log('newJSON >> ', newPrice);

Using Lodash makes your life so much easier that does what you need using lodash there is probably an even more succinct way of doing it with it.
var items = {
"TransactionLine": [
{
"Product": {
"Id": null,
"Codes": [
"1112"
],
"Sku": null
},
"TotalValue": 2.35,
},
{
"Product": {
"Id": null,
"Codes": [
"1113"
],
"Sku": null
},
"TotalValue": 2.15,
}
],
"CustomData": {}
};
var newJson = {
"OrderLines": [
{
"Product": {
"Id": 9,
"Codes": [
"1113"
],
"Sku": "CS1113"
},
"TotalOriginalValue": 0, // asign the price here
},
{
"Product": {
"Id": 21,
"Codes": [
"1112"
],
"Sku": "CS1112"
},
"TotalOriginalValue": 0, // asign the price here
}
]
};
var test = _.map(items.TransactionLine, (item,index) => {
return _.set(newJson.OrderLines[index], 'TotalOriginalValue', item.TotalValue)
})
console.log(test)
https://jsfiddle.net/k6vdyhx7/124/

Iterate over OrderLines key value, which is an array, then replace every TotalOriginalValue value with responding value from the items.TransactionLine array.
var items = {TransactionLine:[{Product:{Id:null,Codes:["1112"],Sku:null},TotalValue:2.35},{Product:{Id:null,Codes:["1113"],Sku:null},TotalValue:2.15}],CustomData:{}},
newJson = {OrderLines:[{Product:{Id:9,Codes:["1113"],Sku:"CS1113"},TotalOriginalValue:0},{Product:{Id:21,Codes:["1112"],Sku:"CS1112"},TotalOriginalValue:0}]};
newJson.OrderLines.forEach((v,i) => v.TotalOriginalValue = items.TransactionLine[i].TotalValue);
console.log(newJson);

it looks like your only connection from JSON A to JSON B is the codes array on the items.
You could loop over entries in JSON a, find the corresponding item in JSON B by checking the codes values, and assign the values directly on JSON B entries
var items = {
"TransactionLine": [
{
"Product": {
"Id": null,
"Codes": [
"1112"
],
"Sku": null
},
"TotalValue": 2.35,
},
{
"Product": {
"Id": null,
"Codes": [
"1113"
],
"Sku": null
},
"TotalValue": 2.15,
}
],
"CustomData": {}
};
var newJson = {
"OrderLines": [
{
"Product": {
"Id": 9,
"Codes": [
"1113"
],
"Sku": "CS1113"
},
"TotalOriginalValue": 0, // asign the price here
},
{
"Product": {
"Id": 21,
"Codes": [
"1112"
],
"Sku": "CS1112"
},
"TotalOriginalValue": 0, // asign the price here
}
]
};
items.TransactionLine.forEach(item=>{
var match = newJson.OrderLines.find(entry=>entry.Product.Codes[0] === item.Product.Codes[0]);
if (!match) {
return;
}
match.TotalOriginalValue = item.TotalValue;
});
console.log(newJson);
This will also cut out the use of the array and a loop through the items JSON.
On a list of 2 its not so bad, but add a few hundred/thousand and it will become noticeable.

Related

How to use find objects in multiple levels of nested arrays

What I want is to find an object in a nested array, and to get it by a pre-known ScheduleId number, and where the Duration property is defined.
We need to find this element which is contained inside of array of "Columns", and "Columns" are contained within "Table" elements.
After finding this object, I want to update ScheduleId = 0 and Duration = 0.
Sample data:
var data = {
"Headers": [
"A",
"B",
"C",
"D"
],
"Table": [
{
"Columns": [
{
"Duration": 0,
"ScheduleId": 12,
},
],
},
{
"Columns": [
{
"Duration": 22,
"ScheduleId": 44,
},
],
},
{
"Columns": [
{
"Duration": 0,
"ScheduleId": 1648,
},
],
},
{
"Columns": [
{
"Duration": 0,
"ScheduleId": 22,
},
],
},
]
};
Pseudo code:
var requestedScheduleId = 22;
var requestedObj = data.Table.find(x => requestedScheduleId.Equals(x.Columns.ScheduleId) )
requestedObj.ScheduleId = 0;
requestedScheduleId.Duration = 0;
Unsuccessful attempt:
var test = data.Table.map(({ Columns }) => {return Columns = Columns.filter(({ ScheduleId }) => ScheduleId == 22 )});
console.log(test);
I would not use .map or .filter for this. It's a plain and simple nested loop: For each table, for each column, if condition is met, do something.
Either with for loops:
for (table of data.Table) {
for (column of table.Columns) {
if (column.ScheduleId === 22) {
column.ScheduleId = 0;
column.Duration = 0;
}
}
}
or with Array#forEach:
data.Table.forEach(table => {
table.Columns.forEach(column => {
if (column.ScheduleId === 22) {
column.ScheduleId = 0;
column.Duration = 0;
}
});
});
var data = {
"Headers": [
"A",
"B",
"C",
"D"
],
"Table": [
{
"Columns": [
{
"Duration": 0,
"ScheduleId": 12,
},
],
},
{
"Columns": [
{
"Duration": 22,
"ScheduleId": 44,
},
],
},
{
"Columns": [
{
"Duration": 0,
"ScheduleId": 1648,
},
],
},
{
"Columns": [
{
"Duration": 0,
"ScheduleId": 22,
},
],
},
]
};
var requestedScheduleId = 22;
data.Table.forEach(table => {
table.Columns.forEach(column => {
if (column.ScheduleId === requestedScheduleId) {
column.ScheduleId = 0;
column.Duration = 0;
}
});
});
console.log(data);

How to parse a JSON (Google Analytics API 4)

I have an API response in form of JSON.
"reports": [
{
"columnHeader": {
"dimensions": [
"ga:date"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:sessions",
"type": "INTEGER"
},
{
"name": "ga:users",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"20210623"
],
"metrics": [
{
"values": [
"13",
"13"
]
}
]
},
{
"dimensions": [
"20210624"
],
"metrics": [
{
"values": [
"18",
"16"
]
}
]
}
]}}]}
I need to get each metric (metricHeaderEntries) with its values in a separate Object, which is therefore is in an array "dataTracesAll".
//Example of the construction
//dataTracesAll is an array, containing objects with key "trace" + int
dataTracesAll['trace' + (i+1)] = {
name: metricsTitles[i].name, //metric title "sessions"
x: dimensions, //list of dimensions ["20210623", "20210624"]
y: dataClear //list of metrics for each metrics is separate ["13", "18"]
}
//The full code:
var titles = [];
var dataTracesAll = [];
//raw data
for (var i=0; i < data.reports.length; i++) {
//get titles
var metricsTitles = data.reports[i].columnHeader.metricHeader.metricHeaderEntries;
metricsTitles.forEach(function(title) {
titles.push(title.name.split("ga:")[1]);
});
//values and dates raw
var dimensions = [];
var dataClear = [];
var values = data.reports[i].data.rows;
//get dates and values
values.forEach(function(val) {
dimensions.push(val.dimensions[0]);
dataClear.push(val.metrics[0].values[0]); //only the first array value is added
});
//clear values
console.log(values);
//constuct array with values
dataTracesAll['trace' + (i+1)] = {
name: metricsTitles[i].name,
x: dimensions,
y: dataClear
}
}
Result of the code:
The problem is that it adds only the first value of the metrics value array and I cannot get how to parse everything, so there is actually 2 traces.
My ideal result is:
dataTracesAll = [
trace1: {
name: "ga:sessions",
x: ['20210623', '20210624']
y: ['13', '18']
},
trace2: {
name: "ga:users",
x: ['20210623', '20210624']
y: ['13', '16']
}
];
Try this:
var data = {"reports": [
{
"columnHeader": {
"dimensions": [
"ga:date"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:sessions",
"type": "INTEGER"
},
{
"name": "ga:users",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"20210623"
],
"metrics": [
{
"values": [
"13",
"13"
]
}
]
},
{
"dimensions": [
"20210624"
],
"metrics": [
{
"values": [
"18",
"16"
]
}
]
}
]}}]};
var titles = [];
var dataTracesAll = [];
var length = data.reports[0].data.rows[0].metrics[0].values.length;
//raw data
for (var i=0; i < length; i++) {
//get titles
var metricsTitles = data.reports[0].columnHeader.metricHeader.metricHeaderEntries;
metricsTitles.forEach(function(title) {
titles.push(title.name.split("ga:")[1]);
});
//values and dates raw
var dimensions = [];
var dataClear = [];
var values = data.reports[0].data.rows;
//get dates and values
values.forEach(function(val) {
dimensions.push(val.dimensions[0]);
dataClear.push(val.metrics[0].values[i]);
});
//constuct array with values
dataTracesAll.push({});
dataTracesAll[i]['trace' + (i+1)] = {
name: metricsTitles[i].name,
x: dimensions,
y: dataClear
}
}
console.log(dataTracesAll);
Edit: The result was supposed to be an array, so I changed the code accordingly.
I have updated you logic to make it fit for your requirement. Hope this will work.
const data =
{
"reports": [
{
"columnHeader": {
"dimensions": [
"ga:date"
],
"metricHeader": {
"metricHeaderEntries": [
{
"name": "ga:sessions",
"type": "INTEGER"
},
{
"name": "ga:users",
"type": "INTEGER"
}
]
}
},
"data": {
"rows": [
{
"dimensions": [
"20210623"
],
"metrics": [
{
"values": [
"13",
"13"
]
}
]
},
{
"dimensions": [
"20210624"
],
"metrics": [
{
"values": [
"18",
"16"
]
}
]
}
]
}
}]
}
const dataTracesAll = {};
const report = data.reports[0];
for (var i = 0; i < report.data.rows.length; i++) {
dataTracesAll[`trace${i + 1}`] = {
name: report.columnHeader.metricHeader.metricHeaderEntries[i].name,
x: [],
y: [],
}
}
Object.keys(dataTracesAll).forEach((key, index) => {
for (var i = 0; i < report.data.rows.length; i++) {
dataTracesAll[key].x.push(report.data.rows[i].dimensions[0]);
dataTracesAll[key].y.push(report.data.rows[i].metrics[0].values[index]);
}
})
console.log(dataTracesAll);

Slicing an Array and producing an object from it

I have an array and it looks as follow:
[
{
"DT_RowId": "row_4758",
"companies": {
"id": 23,
"email": null,
"name": "test"
},
"USERS": {
"UserId": 23
}
},.....
]
How do I slice it and get only "companies": and the result as follows:
[
{
"id": 23,
"email": null,
"name": "test"
},.....
]
to clear some issues I have added the function in which I'm using data.map
fn.loadData = function (data) {
var dataKeys = Object.keys(data);
console.log(data)// 'data' is an object
console.log(data.map(x => x.companies)) ///data.map not a function error
var infiniteList = document.getElementById('infinite-list');
infiniteList.delegate = {
createItemContent: function (i) {
return ons._util.createElement(
'<ons-list-item modifier="chevron" tappable>' + data[dataKeys[i]].name + '</ons-list-item>'
);
},
countItems: function () {
return Object.keys(data).length;
}
};
infiniteList.refresh();
}
as comments told you to do:
const data = [
{
"DT_RowId": "row_4758",
"companies": {
"id": 23,
"email": null,
"name": "test"
},
"USERS": {
"UserId": 23
}
},
{
"DT_RowId": "row_3758",
"companies": {
"id": 24,
"email": null,
"name": "test3"
},
"USERS": {
"UserId": 24
}
},]
console.log(data.map(obj=>obj.companies))
This worked:
const newArray = [];
for (let i = 0; i < companyArray.length; i++) {
newArray.push(companyArray[i].companies);
}
Thanks, everyone

How do I iterate through nested properties of an json object and create a new array list?

I have an json object 'items' and i'm trying to get all the 'values' into a separate string:
"items":[
{
"id":0,
"categoryId":0,
"label":"TOTAL EDUCATION",
"total":739599,
"values":[
451383,
288216
],
"items":[ ],
"metadataIds":"20006",
"collapsed":true
},
{
"id":0,
"categoryId":0,
"label":"TOTAL HIGHWAYS",
"total":63678,
"values":[
32672,
31006
],
"items":[ ],
"metadataIds":"20022",
"collapsed":true
},
for (var i = 0; i < obj.items.length; i++) {
var cost = obj.items[i].values;
}
the output i'm trying to achieve from the values:
[451383,288216],[32672,31006] etc.
Example here:https://jsfiddle.net/zidski/6mg8fszj/
Currently I can only output 1 of the 'values' set.
Currently I can only output 1 of the 'values' set.
Well, yes; you're overwriting it every time.
You've said you want "one string," and the output you've shown has [ and ] in it. Amusingly, while you're not dealing with JSON when iterating, JSON can play a role in producing that string:
var str = obj.items.map(function(item) {
return JSON.stringify(item.values);
}).join(",");
Array#map loops through items building a new array out of what we return for each item from our callback; in the above, we're returning the JSON string equivalent of each item's values array. Then we join the resulting array with commas in-between to get the final string.
Example:
var obj = {
"items": [{
"id": 0,
"categoryId": 0,
"label": "TOTAL EDUCATION",
"total": 739599,
"values": [
451383,
288216
],
"items": [],
"metadataIds": "20006",
"collapsed": true
}, {
"id": 0,
"categoryId": 0,
"label": "TOTAL HIGHWAYS",
"total": 63678,
"values": [
32672,
31006
],
"items": [],
"metadataIds": "20022",
"collapsed": true
}]
};
var str = obj.items.map(function(item) {
return JSON.stringify(item.values);
}).join(",");
document.body.innerHTML =
"<pre>" + str + "</pre>";
If you don't actually want a string, but instead you want an array of arrays, it's a bit simpler:
var arrayOfArrays = obj.items.map(function(item) {
return item.values;
});
var obj = {
"items": [{
"id": 0,
"categoryId": 0,
"label": "TOTAL EDUCATION",
"total": 739599,
"values": [
451383,
288216
],
"items": [],
"metadataIds": "20006",
"collapsed": true
}, {
"id": 0,
"categoryId": 0,
"label": "TOTAL HIGHWAYS",
"total": 63678,
"values": [
32672,
31006
],
"items": [],
"metadataIds": "20022",
"collapsed": true
}]
};
var arrayOfArrays = obj.items.map(function(item) {
return item.values;
});
// Just using JSON so we have text to show as a result
document.body.innerHTML =
"<pre>" + JSON.stringify(arrayOfArrays, null, 2) + "</pre>";

Compare two JSON Arrays and rearrange new JSON Array format

Here is the my first JSON Array format...
[
{
"id": "1234",
"caption": "caption1"
},
{
"id": "2345",
"caption": "caption2"
},
{
"id": "3456",
"caption": "caption3"
}
]
and here is another JSON Array Format
[
[
{
"id": "1234",
"value": "value11"
},
{
"id": "2345",
"value": "value12"
},
{
"id": "3456",
"value": "value13"
}
],
[
{
"id": "1234",
"value": "value21"
},
{
"id": "2345",
"value": "value22"
},
{
"id": "3456",
"value": "value23"
}
]
]
The above mentioned Two JSON Arrays, i need to compare each one with Id and need to format a new JSON Array with caption and value using javascript.
[
[
{
"caption" : "caption1",
"value":"value11"
},
{
"caption" : "caption2",
"value":"value12"
},
{
"caption" : "caption3",
"value":"value13"
}
],
[
{
"caption" : "caption1",
"value":"value21"
},
{
"caption" : "caption2",
"value":"value22"
},
{
"caption" : "caption3",
"value":"value23"
}
]
]
Please help me out.
You can do it in many ways. Below I show two variants:
Option 1: Pure JavaScript
In this example the program preindex first array for faster access to it data, and then loops over second array with map() function to create new array of arrays:
// Create index version of first array
var aix = {};
for(var i=0;i<arr1.length;i++) {
aix[arr1[i].id] = arr1[i].caption;
}
// Loop over array of arrays
var res1 = arr2.map(function(arr22){
return arr22.map(function(a){
return {caption:aix[a.id], value:a.value};
}
});
Option 2: Using special SQL library (Alasql)
Here, you can JOIN to arrays automatically with special SQL statement:
var res2 = arr2.map(function(a){
return alasql('SELECT arr1.caption, a.[value] \
FROM ? a JOIN ? arr1 USING id',[a,arr1]);
});
You can try these variants in working snippet below or play with it in jsFiddle.
(Disclaimer: I am the author of Alasql)
var arr1 = [
{
"id": "1234",
"caption": "caption1"
},
{
"id": "2345",
"caption": "caption2"
},
{
"id": "3456",
"caption": "caption3"
}
];
var arr2 = [
[
{
"id": "1234",
"value": "value11"
},
{
"id": "2345",
"value": "value12"
},
{
"id": "3456",
"value": "value13"
}
],
[
{
"id": "1234",
"value": "value21"
},
{
"id": "2345",
"value": "value22"
},
{
"id": "3456",
"value": "value23"
}
]
];
// JavaScript version
var aix = {};
for(var i=0;i<arr1.length;i++) {
aix[arr1[i].id] = arr1[i].caption;
}
var res1 = arr2.map(function(arr22){
return arr22.map(function(a){
return {caption:aix[a.id], value:a.value};
});
});
document.getElementById("res1").textContent = JSON.stringify(res1);
// Alasql version
var res2 = arr2.map(function(a){
return alasql('SELECT arr1.caption, a.[value] FROM ? a JOIN ? arr1 USING id',[a,arr1]);
});
document.getElementById("res2").textContent = JSON.stringify(res2);
<script src="http://alasql.org/console/alasql.min.js"></script>
<p>Varian 1: JavaScript</p>
<div id="res1"></div>
<p>Variant 2: Alasql</p>
<div id="res2"></div>

Categories