Parse JSON object for unique records - javascript

I have following JSON object:
var data =JSON.parse('[{"Title":"Test 1","State":"Colorado"},
{"Title":"Test 1","State":"Arizona"},
{"Title":"Test 2","State":"Utah"},
{"Title":"Test 2","State":"Arizona"},
{"Title":"Test 3","State":"Arizona"}]');
How do I parse this data object so that I get following output:
resultData = [{"Title":"Test 1", State: ["Colorado", "Arizona"]},
{"Title":"Test 2", State: ["Utah", "Arizona"]},
{"Title":"Test 3", State: ["Arizona"]}]
So far I tried following:
var resultData = {},
groupBy = "Title";
for (var i = 0; i < data.length; i++) {
if (!resultData[data[i][groupBy]])
resultData[data[groupBy]] = [];
resultData[data[i][groupBy]].push(data[i]);
};
but it didn't help and returning some odd object as below:
resultData = [{Test 1: [{State: "Colorado"}, {State: "Arizona"}]},
{Test 2: [{State: "Utah"}, {State: "Arizona"}]},
{Test 3: [{State: "Arizona"}]}]
Can someone help me achieve the same.

what are you trying to achieve is a perfect case for reduce:
Step 1 - parse data as you already did
var data =JSON.parse('[{"Title":"Test 1","State":"Colorado"},
{"Title":"Test 1","State":"Arizona"},
{"Title":"Test 2","State":"Utah"},
{"Title":"Test 2","State":"Arizona"},
{"Title":"Test 3","State":"Arizona"}]');
Step 2 - combine states with same title into array
var titles = data.reduce(function(acc, item){
var title = item.Title;
var state = item.State;
if (!Object.prototype.hasOwnProperty.call(acc, title)){
acc[title] = [];
}
acc[title].push(state);
return acc;
}, {});
Step 3 - build final array using combined states
var resultData = Object.keys(titles).map(function(title){
return {
Title: title,
State: titles[title]
}
});
var data = JSON.parse('[{"Title":"Test 1","State":"Colorado"}, {"Title":"Test 1","State":"Arizona"},{"Title":"Test 2","State":"Utah"},{"Title":"Test 2","State":"Arizona"},{"Title":"Test 3","State":"Arizona"}]');
var titles = data.reduce(function(acc, item) {
var title = item.Title;
var state = item.State;
if (!Object.prototype.hasOwnProperty.call(acc, title)) {
acc[title] = [];
}
acc[title].push(state);
return acc;
}, {});
var resultData = Object.keys(titles).map(function(title) {
return {
Title: title,
State: titles[title]
}
});
console.log(resultData)
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could use a single loop approach with a closure over a hash table for the same groups.
var data = [{ Title: "Test 1", State: "Colorado" }, { Title: "Test 1", State: "Arizona" }, { Title: "Test 2", State: "Utah" }, { Title: "Test 2", State: "Arizona" }, { Title: "Test 3", State: "Arizona" }],
key = 'Title',
grouped = data.reduce(function (group) {
return function (r, o) {
if (!group[o[key]]) {
group[o[key]] = [];
r.push({ Title: o.Title, State: group[o[key]] });
}
group[o[key]].push(o.State);
return r;
};
}(Object.create(null)), []);
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Related

JavaScript get unique array object data by 2 object unique

I have problem with find uniqueness by 2 value. I want do something like SQL GROUP BY Tag_No and PlatformID. I want find unique value by Tag_No and PlayformID where both value can't be duplicate
I have tried something like below, but it only works for one unique 'Tag_No'
var NewTag = [
{Tag_No:'xxx01',PlatformID:'12',Details:'example1'},
{Tag_No:'xxx02',PlatformID:'13',Details:'example2'},
{Tag_No:'xxx03',PlatformID:'14',Details:'example3'},
{Tag_No:'xxx05',PlatformID:'5',Details:'example4'},
{Tag_No:'xxx05',PlatformID:'12',Details:'example5'},
{Tag_No:'xxx05',PlatformID:'12',Details:'example6'},
]
var tmp = [];
var result = [];
if (NewTag !== [] /* any additional error checking */ ) {
for (var i = 0; i < NewTag.length; i++) {
var val = NewTag[i];
if (tmp[val.Tag_No] === undefined ) {
tmp[val.Tag_No] = true;
result.push(val);
}
}
}
console.log('result',result)
expected value is
result=[{Tag_No:'xxx01',PlatformID:'12',Details:'example1'},
{Tag_No:'xxx02',PlatformID:'13',Details:'example2'},
{Tag_No:'xxx03',PlatformID:'14',Details:'example3'},
{Tag_No:'xxx05',PlatformID:'5',Details:'example4'},
{Tag_No:'xxx05',PlatformID:'12',Details:'example5'},
]
use array.filter instead.
This filters your array on duplicates no matter what structure you have.
Reference
var NewTag = [
{Tag_No:'xxx01',PlatformID:'12',Details:'example'},
{Tag_No:'xxx02',PlatformID:'13',Details:'example'},
{Tag_No:'xxx03',PlatformID:'14',Details:'example'},
{Tag_No:'xxx05',PlatformID:'5',Details:'example'},
{Tag_No:'xxx05',PlatformID:'12',Details:'example'},
{Tag_No:'xxx05',PlatformID:'12',Details:'example'},
]
const uniqueArray = NewTag.filter((value, index) => {
const _value = JSON.stringify(value);
return index === NewTag.findIndex(obj => {
return JSON.stringify(obj) === _value;
});
});
console.log('result',uniqueArray)
You can use hash grouping approach:
const data = [{Tag_No:'xxx01',PlatformID:'12',Details:'example'},{Tag_No:'xxx02',PlatformID:'13',Details:'example'},{Tag_No:'xxx03',PlatformID:'14',Details:'example'},{Tag_No:'xxx05',PlatformID:'5',Details:'example'},{Tag_No:'xxx05',PlatformID:'12',Details:'example'},{Tag_No:'xxx05',PlatformID:'12',Details:'example'}];
const result = Object.values(data.reduce((acc, item) => {
const hash = [item.Tag_No, item.PlatformID].join('---');
acc[hash] ??= item;
return acc;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }
Here is my solution:
let NewTag = [
{Tag_No:'xxx01',PlatformID:'12',Details:'example'},
{Tag_No:'xxx02',PlatformID:'13',Details:'example'},
{Tag_No:'xxx03',PlatformID:'14',Details:'example'},
{Tag_No:'xxx05',PlatformID:'5',Details:'example'},
{Tag_No:'xxx05',PlatformID:'12',Details:'example'},
{Tag_No:'xxx05',PlatformID:'12',Details:'example'},
]
let temp=[]
let result=[];
NewTag.forEach(tag=>{
let key=tag.Tag_No+"\t"+tag.PlatformID;
if (!temp.includes(key)){
temp.push(key);
result.push(tag)
}
});
console.log(result)
You could use Set to check for uniqueness
const NewTag = [
{ Tag_No: "xxx01", PlatformID: "12", Details: "example" },
{ Tag_No: "xxx02", PlatformID: "13", Details: "example" },
{ Tag_No: "xxx03", PlatformID: "14", Details: "example" },
{ Tag_No: "xxx05", PlatformID: "5", Details: "example" },
{ Tag_No: "xxx05", PlatformID: "12", Details: "example" },
{ Tag_No: "xxx05", PlatformID: "12", Details: "example" },
]
const uniquePairSet = new Set()
const res = NewTag.reduce((acc, el) => {
const Tag_No_PlatformID = `${el.Tag_No}-${el.PlatformID}`
if (!uniquePairSet.has(Tag_No_PlatformID)) {
uniquePairSet.add(Tag_No_PlatformID)
acc.push(el)
}
return acc
}, [])
console.log("result", res)
References
Set

Updating JSON structure based on id

I was trying to update the town name in the below-given JSON structure.
"City":[
{
"Name":"Delhi",
"id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
"Towns":[
{
"Name":"MG Colony",
"conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1a4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
},
{
"Name":"DLF Colony",
"conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1a4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
}
]
},
{
"Name":"Pune",
"id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax",
"Towns":[
{
"Name":"Hadapsar",
"conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1x4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax"
},
{
"Name":"Magarpatta",
"conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1f4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bax"
}
]
}
]
I wanted to change the town name from "Hapdasar" to "Viman Nagar" if my cid matches that of Hadapsar Town
Output which I wanted was:
"City":[
{
"Name":"Delhi",
"id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
"Towns":[
{
"Name":"MG Colony",
"conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1a4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
},
{
"Name":"DLF Colony",
"conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1a4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
}
]
},
{
"Name":"Pune",
"id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax",
"Towns":[
{
"Name":"Viman Nagar",
"conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1x4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax"
},
{
"Name":"Magarpatta",
"conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1f4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bax"
}
]
}
]
I was using js map to iterate but was confused about how to replicate the exact structure.
Well, map alone is not enough to solve your problem, since you have two nested arrays. Maybe you can consider the possibility to use maptwice?
For example:
var cid = "c5d58bef-f1c2-4b7c-a6d7-f64df12321ax";
var newName = "Viman Nagar";
City = City.map(function(city) {
city.towns = city.towns.map(function(town) {
if (town.cid === cid)
town.name = newName;
return town;
});
return city;
});
Atribute your object for
let cities = [{
"Name": "Delhi"
...
}]
and then you can map over it
let result = cities.map(city => city.Towns.map(town => {
if (town.Name === "Hadapsar") {
return {
...town,
Name: "Viman Nagar"
}
} else return town
}))
Use Array#map as follows:
Iterate over cities
In every iteration, iterate over Towns. If current town's cid is equal to the one to change, update its Name
const changeTownName = (cities = [], cid, name) =>
cities.map(({ Towns = [], ...props }) => ({
...props,
Towns: Towns.map(town => town.cid === cid
? { ...town, Name: name }
: { ...town }
)
}));
const cities = [
{ Name: 'Delhi', id: 'c5d58bef-f1c2-4b7c-a6d7-f64df12321bd', Towns: [ { Name: "MG Colony", conditionId: '60d1f5eb-0222-4a84-879b-6699b0cfc1a4', cid: 'c5d58bef-f1c2-4b7c-a6d7-f64df12321bd' }, { Name: "DLF Colony", conditionId: '60d1f5eb-0222-4a84-879b-7749b0cfc1a4', cid: 'c5d58bef-f1c2-4b7c-a6d7-f64df12321bd' } ] },
{ Name: 'Pune', id: 'c5d58bef-f1c2-4b7c-a6d7-f64df12321ax', Towns: [ { Name: "Hadapsar", conditionId: '60d1f5eb-0222-4a84-879b-6699b0cfc1x4', cid: 'c5d58bef-f1c2-4b7c-a6d7-f64df12321ax' }, { Name: "Magarpatta", conditionId: '60d1f5eb-0222-4a84-879b-7749b0cfc1f4', cid: 'c5d58bef-f1c2-4b7c-a6d7-f64df12321bax' } ] }
];
console.log( changeTownName(cities, 'c5d58bef-f1c2-4b7c-a6d7-f64df12321ax', 'Viman Nagar') );
If you consider city as cities this code can help you;
cities.forEach(city => {
city.Towns = city.Towns.map(el => {
if (el.Name === 'Hapdasar') {
el.Name = 'Viman Nagar';
}
return el;
})
});
You'll need to loop for each city in your array, and each town in the city. If the cid matches the town's cid, then change it;
const myNewTownName = "Viman Nagar";
const cid = "c5d58bef-f1c2-4b7c-a6d7-f64df12321ax";
for(let i = 0; i < myObj.City.length; i++){
const city = myObj.City[i];
for(let j = 0; j < city.Towns.length; j++){
const town = city.Towns[j];
if(cid === town.cid){
town.Name = myNewTownName;
}
city.town[j] = town;//Updates city with the updated town
}
myObj.City[i] = city; //Updates myObj with the updated city
}
The result can also be obtained using nested .forEach loops to parsing through the outer and inner arrays, with an if block to examine the cid for the target town.
const data = {
"City":[
{
"Name":"Delhi",
"id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
"Towns":[
{
"Name":"MG Colony",
"conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1a4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
},
{
"Name":"DLF Colony",
"conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1a4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
}
]
},
{
"Name":"Pune",
"id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax",
"Towns":[
{
"Name":"Hadapsar",
"conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1x4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax"
},
{
"Name":"Magarpatta",
"conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1f4",
"cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bax"
}
]
}
]
} // end data;
const targetCid = "c5d58bef-f1c2-4b7c-a6d7-f64df12321ax"; // cid for Hadapsar;
const objArray = data.City;
objArray.forEach(element => {
element.Towns.forEach(element => {
if (element.cid == targetCid) {
element.Name = "Viman Nagar";
} // end if;
}); // next object in Towns array;
}); // next object in objArray;
document.getElementById('output').textContent = JSON.stringify(data, undefined, 2);
#output {
white-space: pre;
}
<pre id="output"></pre>

Converting an array of data to JSON using a property map

I am trying to convert a 2d array into a json object using a key map.
The key map looks like
var keys = ['id', 'title', 'customer.id', 'customer.name', 'customer.phone.home', 'customer.phone.mobile' ];
and the data is
var data = [
[1, 'Task 1', 'C1', 'Customer 1', '999', '8888'],
[2, 'Task 2', 'C2', 'Customer 2', '333', '5555']
];
Output JSON should be
var output = [
{
"id":1,
"title":"Task 1",
"customer":{
"id":"C1",
"name":"Customer 1",
"phone":{
"home":"999",
"mobile":"8888"
}
}
},
{
"id":2,
"title":"Task 2",
"customer":{
"id":"C2",
"name":"Customer 2",
"phone":{
"home":"333",
"mobile":"5555"
}
}
}
];
I am trying to do it something like but I am not good here making smerecursion etc. Could anyone help please?
function arrToJSON(headers, data){
var output = [];
data.forEach(row, index){
var cObj = {};
headers.forEach(header, itemIndex){
var headerParts = header.split('.');
// NOt sure what to do here
}
}
}
You can easily achieve the result using map and reduce in js.
createObj(acc, curr.split("."), 0, o[index]);
is the function that is used in recursion and that is what you're looking for.
Arguments
createObj(
acc, // object in which you want to add value
curr.split("."), // send path as an array
0, // current index in path, initially zero
o[index] // value to be assigned
);
var keys = [
"id",
"title",
"customer.id",
"customer.name",
"customer.phone.home",
"customer.phone.mobile",
];
var data = [
[1, "Task 1", "C1", "Customer 1", "999", "8888"],
[2, "Task 2", "C2", "Customer 2", "333", "5555"],
];
function createObj(obj, arr, index, value) {
if (index === arr.length - 1) obj[arr[index]] = value;
else {
if (!obj[arr[index]]) obj[arr[index]] = {};
createObj(obj[arr[index]], arr, index + 1, value);
}
}
const result = data.map((o) => {
return keys.reduce((acc, curr, index) => {
createObj(acc, curr.split("."), 0, o[index]);
return acc;
}, {});
});
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}
You can use simply use destructure and spread operator with reduce.
var data = [
[1, "Task 1", "C1", "Customer 1", "999", "8888"],
[2, "Task 2", "C2", "Customer 2", "333", "5555"],
];
const buildObject = (arr = []) => {
return arr.reduce((acc, [id, title, cid, name, home, mobile]) => {
const row = {
id,
title,
customer: { id: cid, name, phone: { home, mobile } },
};
return acc.concat(row);
}, []);
};
console.log(buildObject(data));

How to convert array of object into an array of object with different format?

I'm having an array of object,in which I'm storing the billkey and billvalue as attributes. I want billkey to be the key and billvalue to be the value of that particular key.
var log=[
{
billkey:"Name",
billvalue:"ABC"
},
{
billkey:"Department",
billvalue:"Computer"
}
{
billkey:"Name",
billvalue:"XYZ"
},
{
billkey:"Department",
billvalue:"Electrical"
}];
And I want to convert it into this format:
var log=[
{
Name:"ABC",
Department:"Computer"
},
{
Name:"XYZ",
Department:"Electrical"
}];
How about this simple solution. Hope it helps!
var log=[
{
billkey:"Name",
billvalue:"ABC"
},
{
billkey:"Department",
billvalue:"Computer"
},
{
billkey:"Name",
billvalue:"XYZ"
},
{
billkey:"Department",
billvalue:"Electrical"
}];
var arr = [];
var finalObj = [];
for(var i in log){
var someObject = log[i];
for(var j in someObject){
arr.push(someObject[j]);
}
}
for(var k = 0; k < arr.length; k+=4){
finalObj.push({
Name: arr[k+1],
Department: arr[k+3]
});
}
console.log(finalObj);
create the result using forloop
// store the values
var logs=[];
var log=[
{
billkey:"Name",
billvalue:"ABC"
},
{
billkey:"Department",
billvalue:"Computer"
},
{
billkey:"Name",
billvalue:"XYZ"
},
{
billkey:"Department",
billvalue:"Electrical"
},
];
loop the first array
for (i = 0; i < log.length; i++) {
// create empty variable for storing the values
var index = new Array();
// insert the first index value to key
index[log[i].billkey] = log[i].billvalue
// insert the second index value to key
index[log[i+1].billkey] = log[i+1].billvalue
// insert the result in to new array
logs.push(index);
// increment the i with 1
i=i+1;
}
console.log(logs);
You could use Array#reduce and use the remainder operator as witch for using either the last object or create a new one.
var log = [{ billkey: "Name", billvalue: "ABC" }, { billkey: "Department", billvalue: "Computer" }, { billkey: "Name", billvalue: "XYZ" }, { billkey: "Department", billvalue: "Electrical" }],
result = log.reduce(function (r, a, i) {
var o = {};
if (i % 2) {
r[r.length - 1][a.billkey] = a.billvalue;
} else {
o[a.billkey] = a.billvalue;
r.push(o);
};
return r;
}, []);
console.log(result);

JavaScript Get values from array but ignore duplicates

I have an array that contains different clothes and the type of the cloth. For example, I may have a specific shirt that belongs to the shirts category. What I want to do is get all the types from an array and ignore any duplicate entries. So if I have 3 shirts and 2 trousers, I will only get 1 shirt and 1 trouser.
array = [
{
name: "test 1",
type: "shirt"
},
{
name: "test 2",
type: "shirt"
},
{
name: "test 3",
type: "trousers"
},
{
name: "test 4",
type: "trousers"
}
];
var categories = {};
for(var i = 0, len = array.length; i < len; i++) {
if(categories.indexOf(array[i].type) > -1) {
console.log('Duplicate type');
}
else {
console.log('New type');
categories.push(array[i].type);
}
}
But I end up getting TypeError: categories.indexOf is not a function.
Pretty short solution using ES6 Set object:
The Set object lets you store unique values of any type, whether
primitive values or object references.
var categories = new Set();
array.forEach((o) => categories.add(o.type));
categories = [...categories]; // Set to Array transformation
console.log(categories); // ["shirt", "trousers"]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
You need an array for categories, not an object.
var categories = [];
array = [
{
name: "test 1",
type: "shirt"
},
{
name: "test 2",
type: "shirt"
},
{
name: "test 3",
type: "trousers"
},
{
name: "test 4",
type: "trousers"
}
];
var categories = [];
for(var i = 0, len = array.length; i < len; i++) {
if(categories.indexOf(array[i].type) > -1) {
console.log('Duplicate type');
}
else {
console.log('New type');
categories.push(array[i].type);
}
}
console.log(categories);
This happens because you define categories as object literal ({}), rather than an array ([]):
// --------------vv
var categories = {};
Your issue is that you are trying to invoke .push method on an object but the method is available only on Array. You need to make categories an array in order to push to it.
As an alternative, you can use pure function without any mutations using Array.prototype.reduce() to reduce the array of duplicate objects to unique ones:
var array = [
{
name: "test 1",
type: "shirt"
},
{
name: "test 2",
type: "shirt"
},
{
name: "test 3",
type: "trousers"
},
{
name: "test 4",
type: "trousers"
}
];
function unique(input) {
return input.reduce(function (acc, current) {
if (acc.indexOf(current.type) > -1) {
return acc
} else {
return acc.concat([current.type]);
}
}, [])
}
var categories = unique(array);
console.log(categories);
If you want to see the result of every row then I think first implementation could be the answer but if you want just the categories then using map make it simple.
array = [
{ name: "test 1", type: "shirt" },
{ name: "test 2", type: "shirt" },
{ name: "test 3", type: "trousers" },
{ name: "test 4", type: "trousers" }
];
// --------------------------------------
var categories = [];
array.forEach(v => {
if (this[v.type])
return console.log('Duplicate type');
console.log('New type');
this[v.type] = true;
categories.push(v.type);
}, {});
console.log(categories);
// --------------------------------------
var map = new Map;
array.forEach(v => map.set(v.type, v.type));
categories = [...map.keys()];
console.log(categories);

Categories