Object manipulation - javascript

I am using either PHP or Javascript to manipulate an json object from API sample:
{
"data": [
{
"label": "employeeCount",
"stats": [
{
"year": "2015",
"value": "10"
},
{
"year": "2017",
"value": "30"
},
{
"year": "2016",
"value": "50"
}
]
},
{
"label": "managerCount",
"stats": [
{
"year": "2015",
"value": "2"
},
{
"year": "2017",
"value": "4"
},
{
"year": "2016",
"value": "6"
}
]
}
]
}
I need to categorize it by the year as object like such:
"record": {
"2015" : {
"employeeCount": "10",
"managerCount": "2"
},
"2016" : {
"employeeCount": "30",
"managerCount": "4"
},
"2017"{
"employeeCount": "50",
"managerCount": "6"
}
}
The number of year and the number of label will be different from the API call so I am thinking about using a for loop to make it happen. But so far no success. What approach would you use for this kind of manipulation?

Use Array.forEach
LOGIC - Idea is to iterate over data array in object and then for each entry in data array, iterate over its corresponding stats array to populate the object based on year. While iterating over the stat array, check for existing entry in resultant object. If does not exist, create an entry for it. Update the entry by adding the label as key and value from its corresponding object in stat array.
let obj = {"data":[{"label":"employeeCount","stats":[{"year":"2015","value":"10"},{"year":"2017","value":"30"},{"year":"2016","value":"50"}]},{"label":"managerCount","stats":[{"year":"2015","value":"2"},{"year":"2017","value":"4"},{"year":"2016","value":"6"}]}]};
// Create the response object
let r = {"record":{}};
// Iterate over data array
obj.data.forEach(o => {
// Iterate over stats for each object in data array
o.stats.forEach(s => {
// Create entry for year in result object if it does not exist
r.record[s.year] = r.record[s.year] || {};
// Add the label of data array object with corresponding stat value in resultant object
r.record[s.year][o.label] = s.value;
});
});
console.log(r);

let data = json.data
let map={}
for(let i=0;i<data.length;i++){
let stats = data[i].stats
for(let j=0;j<stats.length;j++){
let it = stats[j]
if(!map[it.year]){
map[it.year]={}
}
map[it.year][data[i].label]=it.value
}
}
console.log({record:map})

Related

Nested array issue in JavaScript

I have the following array
Array["MyArray",
{
"isLoaded":true,
"items":
[{
"id":"4",
"name":"ProductA",
"manufacturer":"BrandA",
"quantity":1,
"price":"25"
},{
"id":"1",
"name":"ProductB",
"manufacturer":"BrandB",
"quantity":5,
"price":"20"
}],
"coupons":null
}
]
I need to load product names and their quantity from the array.
const result = [key, value].map((item) => `${item.name} x ${item.quantity}`);
Here's one possible way to achieve the desired result:
const getProductsAndQuantity = ([k , v] = arr) => (
v.items.map(it => `${it.name} x ${it.quantity}`)
);
How to use it within the context of the question?
localforage.iterate(function(value, key, iterationNumber) {
console.log([key, value]);
const val2 = JSON.parse(value);
if (val2 && val2.items && val2.items.length > 0) {
console.log(val2.items.map(it => `${it.name} x ${it.quantity}`).join(', '))
};
});
How it works?
Among the parameters listed in the question ie, value, key, iterationNumber, only value is required.
The above method accepts the key-value pair as an array (of 2 elements) closely matching the console.log([key, value]); in the question
It uses only v (which is an object). On v, it accesses the prop named items and this items is an Array.
Next, .map is used to iterate through the Array and return each product's name and quantity in the desired/expected format.
Test it out on code-snippet:
const arr = [
"MyArray",
{
"isLoaded": true,
"items": [{
"id": "4",
"name": "ProductA",
"manufacturer": "BrandA",
"quantity": 1,
"price": "25"
}, {
"id": "1",
"name": "ProductB",
"manufacturer": "BrandB",
"quantity": 5,
"price": "20"
}],
"coupons": null
}
];
const getProductsAndQuantity = ([k, v] = arr) => (
v.items.map(
it => `${it.name} x ${it.quantity}`
)
);
console.log(getProductsAndQuantity());
I understood. You should learn about array methods such as map, filter, reduce. Here you go...
const items = [{
"id":"4",
"name":"ProductA",
"manufacturer":"BrandA",
"quantity":1,
"price":"25"
},{
"id":"1",
"name":"ProductB",
"manufacturer":"BrandB",
"quantity":5,
"price":"20"
}];
const result = items.map((item) => `${item.name} x ${item.quantity}`);
console.log(result);
I think I understand the question to say that the input is an array of objects, each containing an array of items. The key is that a nested array requires a nested loop. So, we iterate the objects and their internal items (see the lines commented //outer loop and // inner loop below)
Also, half-guessing from the context, it looks like the that the OP aims to assemble a sort of invoice for each object. First a demo of that, (and see below for the version simplified to exactly what the OP asks)...
const addInvoice = obj => {
let total = 0;
// inner loop
obj.invoice = obj.items.map(i => {
let subtotal = i.quantity * i.price;
total += subtotal
return `name: ${i.name}, qty: ${i.quantity}, unit price: ${i.price}, subtotal: ${subtotal}`
});
obj.invoice.push(`invoice total: ${total}`);
}
const objects = [{
"isLoaded": true,
"items": [{
"id": "4",
"name": "ProductA",
"manufacturer": "BrandA",
"quantity": 1,
"price": "25"
}, {
"id": "1",
"name": "ProductB",
"manufacturer": "BrandB",
"quantity": 5,
"price": "20"
}],
"coupons": null
}]
// outer loop
objects.forEach(addInvoice);
console.log(objects);
If my guess about the goal went to far, just remove the unit price, subtotal and total lines from the invoice function...
const objects = [{
"isLoaded": true,
"items": [{
"id": "4",
"name": "ProductA",
"manufacturer": "BrandA",
"quantity": 1,
"price": "25"
}, {
"id": "1",
"name": "ProductB",
"manufacturer": "BrandB",
"quantity": 5,
"price": "20"
}],
"coupons": null
}]
const summaryString = obj => {
return obj.items.map(i => `${i.name}, ${i.quantity}`);
}
const strings = objects.map(summaryString);
console.log(strings);

Restructure nested Object

I have a problem. I have an object with this structure like this example.
{
"Name": "Peter",
"Username": "dummy",
"Age": 18,
"moreData": {
"tags": [1,2,3],
"hasCar": true,
"preferences": {
"colors": ["green", "blue"]
}
}
}
I would like to convert it to an array like the following. I am desperate and can not get any further. I have issues as soon I get some nested objects. Does someone have an idea how I can achieve this? Kind Regards
[
{
"key": "Name",
"val": "Peter"
},
{
"key": "Username",
"val": "dummy"
},
{
"key": "Age",
"val": "18"
},
{
"key": "tags",
"val": [1,2,3]
},
{
"key": "hasCar",
"val": true
},
{
"key": "colors",
"val": ["green", "blue"]
}
]
For this you need to first iterate through all the key value pairs of your object and change the specific type of data into name value pairs except the nested objects. If the value in the object at a certain key is an object then the same procedure has to be done for it. And since there can be N number of levels for this nested data thus we need a recursive function for it. Whenever we have to do a same set of processing for nested data then it always means it can be done using recursion. It can be done via for loops too but a recursive function is much clear and lesser to write.
function getData(data) {
let results = [];
Object.keys(data).forEach(key => {
// If the type of the data item is object and is not an array, go into recursion
if(typeof data[key] == 'object' && !Array.isArray(data[key])) {
results = results.concat(getData(data[key]));
} else {
results.push({ key, val: data[key] });
}
});
return results;
}
const data = {
"Name": "Peter",
"Username": "dummy",
"Age": 18,
"moreData": {
"tags": [1,2,3],
"hasCar": true,
"preferences": {
"colors": ["green", "blue"]
}
}
};
const results = getData(data);
console.log(results);
// [{"key":"Name","val":"Peter"},{"key":"Username","val":"dummy"},{"key":"Age","val":18},{"key":"tags","val":[1,2,3]},{"key":"hasCar","val":true},{"key":"colors","val":["green","blue"]}]

creating an object from JSON data

I have the following data in a .json file:
{
"countries": {
"sweden": {
"currency": "Swedish krona",
"majorLanguage": "Swedish",
"landArea": {
"value": 410330,
"uom": "sq km"
}
},
"japan": {
"currency": "yen",
"majorLanguage": "Japanese",
"landArea": {
"value": 364500,
"uom": "sq km"
}
},
"unitedStatesOfAmerica": {
"currency": "US dollar",
"majorLanguage": "English",
"landArea": {
"value": 3796742,
"uom": "sq mi"
}
}
}
}
and need to come up with a way to create this object from it:
Object {
"currency": Object {
"japan": "yen",
"sweden": "Swedish krona",
"unitedStatesOfAmerica": "US dollar"
},
"majorLanguage": Object {
"japan": "Japanese",
"sweden": "Swedish",
"unitedStatesOfAmerica": "English"
},
"landArea": Object {
"japan": Object {
"value": 364500,
"uom": "sq km"
},
"sweden": Object {
"value": 410330,
"uom": "sq km"
},
"unitedStatesOfAmerica": Object {
"value": 3796742,
"uom": "sq mi"
}
}
}
The app that will be consuming this data is written in Vue so using JavaScript to accomplish this would make sense, although my preference is to not use any third party libraries. Specifically, I'm interested in a programmatic approach that doesn't require hard coding of to manually create objects for currency, majorLanguage, landArea. I don't really know how to start tackling this so don't have any sample attempts to post here.
Nothing fancy here:
const result = {};
for (const name in countries) {
const country = countries[name];
for (const key in country) {
if (!result[key]) result[key] = {};
result[key][name] = country[key];
}
}
You can use Array#reduce method and do something like the following.
const data = {"countries":{"sweden":{"currency":"Swedish krona","majorLanguage":"Swedish","landArea":{"value":410330,"uom":"sq km"}},"japan":{"currency":"yen","majorLanguage":"Japanese","landArea":{"value":364500,"uom":"sq km"}},"unitedStatesOfAmerica":{"currency":"US dollar","majorLanguage":"English","landArea":{"value":3796742,"uom":"sq mi"}}}};
// iterate over countries object key value pair
const res = Object.entries(data.countries).reduce((obj, [country, valObj]) => {
// iterate over the country value key-val pair
Object.entries(valObj).forEach(([key, val]) => {
// define key if not defined
obj[key] = obj[key] || {};
// define the value within object where key is country
obj[key][country] = val;
})
return obj;
}, {})
console.log(res);

Javascript -sort array based on another javascript object properties

I have one javascript array and one object . Need help to sort javascript object keys based on the order number in another array
In subgroup array , I have name , order number. Need to sort Offerings keys based on that order number
const subgroup = [
{
"code": "6748",
"name": "test123",
"orderNumber": "0"
},
{
"code": "1234",
"name": "customdata",
"orderNumber": "1"
}
]
const offerings = {
"customdata" : [
{
"code": "Audi",
"color": "black"
}
],
"test123" : [
{
"brand": "Audi",
"color": "black"
}
]
}
I believe this should work for you. I've added some comments in the code that should hopefully do an okay job of explaining what is happening.
var subgroup = [{
"code": "6748",
"name": "test123",
"orderNumber": "0"
}, {
"code": "1234",
"name": "customdata",
"orderNumber": "1"
}];
var offerings = {
"customdata": [{
"code": "Audi",
"color": "black"
}],
"test123": [{
"brand": "Audi",
"color": "black"
}]
}
function sortObjectFromArray(refArray, sortObject, orderKey = 'order', linkKey = 'key') {
// Get copy of refArray
let reference = refArray.slice();
// Sort sortObject [ into an array at this point ]
let sorted = [];
for (let key in sortObject) {
// Searches the refArray for the linkKey, and returns the intended index
let index = reference.find((item) => item[linkKey] === key)[orderKey];
// Places the sortObject's value in the correct index of the 'sorted' Array
sorted[parseInt(index)] = [key, sortObject[key]];
};
// Return an object, created from previous 'sorted' Array
return sorted.reduce((obj, [key, value]) => {
obj[key] = value;
return obj;
}, {});
};
offerings = sortObjectFromArray(subgroup, offerings, 'orderNumber', 'name');
console.log(offerings);

Access nested members in JSON

I'm trying to access members in a json, however I am running into some trouble. Here is an example of one of the json objects, stored in var obj:
var fs = require('fs');
var obj = [
{
"_id": "52d7f816f96d7f6f31fbb680",
"regNum": "0361300035313000002",
"sd": "2013-01-01T00:00:00",
"pd": "2013-01-25T09:30:29Z",
"prd": "2012-12-18",
"p": 1395000000,
"pt": [
{
"name": name here",
"price": 1395000000,
"OKDP": {
"code": "5520109",
"name": "name here"
},
"sid": "25484812",
"sum": "1395000000",
"OKEI": {
"code": "796",
"name": "name two"
},
"quantity": "1"
}
],
"b": 0,
"c": 0,
"s": 0
}
];
I'm trying to access the sid and sum values, by doing the following:
var sid = [];
var sum = [];
obj.forEach(block => {
var sidOut = block.pt.sid;
var sumOut = block.pt.sum;
sid.push(sidOut);
sum.push(sumOut);
});
console.log(sid);
console.log(sum);
I tried the solution here, however, when I run these it gives me [ undefined ] errors.
Why am I unable to access this two values?
if you see your pt is an array of an object [{}] so you need to select which element you want to access so
var sidOut = block.pt[0].sid;
var sumOut = block.pt[0].sum;
should get you the right result

Categories

Resources