Remove duplicate element in object and leave unique element - javascript

I have two object:
obj1
{
"uuid": "",
"open_bal_qty": 0,
"open_bal_value": 0,
"qty_min": 0,
"qty_med": 0,
"qty_max": 0,
"kedai_uuid": "198ceaef-4ced-4207-9ba0-62afbb42bb85"
}
obj2
{
"uuid": "",
"open_bal_qty": 0,
"open_bal_value": 0,
"kedai_uuid": "198ceaef-4ced-4207-9ba0-62afbb42bb85"
}
How can I remove duplicate and get element not exist in obj2 so my new obj will be:
newObj
{
"qty_min": 0,
"qty_med": 0,
"qty_max": 0,
}
I can use lodash pick but I dont want to manually entered the key name that I want to filter.
const newObj = pick(obj1, [
'qty_min',
'qty_med',
'qty_max',
]);
Thanks in advance.

With lodash you can _.omit() from obj1 all the _.keys() of obj2:
const obj1 = {"uuid":"","open_bal_qty":0,"open_bal_value":0,"qty_min":0,"qty_med":0,"qty_max":0,"kedai_uuid":"198ceaef-4ced-4207-9ba0-62afbb42bb85"};
const obj2 = {"uuid":"","open_bal_qty":0,"open_bal_value":0,"kedai_uuid":"198ceaef-4ced-4207-9ba0-62afbb42bb85"};
const newObj = _.omit(obj1, _.keys(obj2));
console.log(newObj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Use for..in to iterate the object and Object.hasOwnProperty to check if the second object have the same key
var obj1 = {
"uuid": "",
"open_bal_qty": 0,
"open_bal_value": 0,
"qty_min": 0,
"qty_med": 0,
"qty_max": 0,
"kedai_uuid": "198ceaef-4ced-4207-9ba0-62afbb42bb85"
}
var obj2 = {
"uuid": "",
"open_bal_qty": 0,
"open_bal_value": 0,
"kedai_uuid": "198ceaef-4ced-4207-9ba0-62afbb42bb85"
}
var newObj = {};
for (var keys in obj1) {
if (!obj2.hasOwnProperty(keys)) {
newObj[keys] = obj1[keys]
}
}
console.log(newObj)

You could use _.pick in conjunction with _.difference by finding the difference between the two objects' keys and picking those keys:
const obj1 = {
"uuid": "",
"open_bal_qty": 0,
"open_bal_value": 0,
"qty_min": 0,
"qty_med": 0,
"qty_max": 0,
"kedai_uuid": "198ceaef-4ced-4207-9ba0-62afbb42bb85"
};
const obj2 = {
"uuid": "",
"open_bal_qty": 0,
"open_bal_value": 0,
"kedai_uuid": "198ceaef-4ced-4207-9ba0-62afbb42bb85"
};
const obj1Keys = _.keys(obj1); //or Object.keys
const obj2Keys = _.keys(obj2);
console.log(_.pick(obj1Keys > obj2Keys ? obj1 : obj2, _.difference(obj1Keys, obj2Keys)))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

Related

Is there a way I can compare two object using ES7 Set or Map method?

I have two set of javascript objects.
const object1 = {
checkedB: false,
inqueryProcess: "inqury",
inquryChannel: 0,
inquryDate: undefined,
inquryManager: "Option 1",
inquryMemo: "",
inquryRoute: 0,
inquryTitle: 0,
radio1: "common",
radio2: "none"
}
const object2 = {
checkedB: false,
inquryMemo: "",
inquryChannel: 0,
inquryDate: undefined,
inquryTitle: 0,
radio2: "none"
inquryManager: "Option 1",
inqueryProcess: "inqury",
inquryRoute: 0,
radio1: "common",
}
I have two set of objects which exactly the same and I want to compare both whether they have equal value.
Of course I can take advantage of JSON.stringfy but I want to avoid it due to order issue.
Therefore I want to use es7 new Map or Set method to make comparing faster and efficient.
I added these values into new Set like this below.
const newSet1 = new Set();
const newSet2 = new Set();
Object.values(object1).forEach(val => newSet1.add(val));
Object.values(object2).forEach(val => newSet2.add(val))
console.log(newSet1);
console.log(newSet2)
Now newSet1 and newSet2 contain values of object1 and object2.
How can I compare these two whether they have same set of value?
You could take a Map and check the key/value pairs from the other object and delete existing keys. At the end check if the length of the map is zero.
const
isEqual = (a, b) => {
const map = new Map(Object.entries(a));
return Object
.entries(b)
.every(([k, v]) => map.get(k) === v && map.delete(k)) &&
!map.size;
},
object1 = { checkedB: false, inqueryProcess: "inqury", inquryChannel: 0, inquryDate: undefined, inquryManager: "Option 1",inquryMemo: "", inquryRoute: 0, inquryTitle: 0, radio1: "common", radio2: "none" },
object2 = { checkedB: false, inqueryProcess: "inqury", inquryChannel: 0, inquryDate: undefined, inquryManager: "Option 1", inquryMemo: "", inquryRoute: 0, inquryTitle: 0, radio1: "common", radio2: "none" };
console.log(isEqual(object1, object2));

Nested key value pair grouping into an array

I have an Object with nested key value pair as:
Products:{
Clip:{
today_entry: 0,
today_sold: 0
},
Necklace:
{
today_entry: 0,
today_sold: 2
}
}
I want to loop through the Objects Clip ad Necklace and group the values as per their inner keys i.e. today_entry, today_sold into the format:
{
today_entry : [0,0],
today_sold : [0,2]
}
I tried doing it using Object.entries but since it is nested I'm not able to get the inner keys. Could anyone please help me? Thank you.
You can use reudce:
const products = {
Clip:{
today_entry: 0,
today_sold: 0,
},
Necklace:
{
today_entry: 0,
today_sold: 2,
},
};
const result = Object.keys(products).reduce((ac, key) => ({
today_entry: [ ...ac.today_entry, products[key].today_entry],
today_sold: [ ...ac.today_sold, products[key].today_sold],
}), { today_entry: [], today_sold: []});
console.log(result);
In case the order of the values in the arrays are important you should also sort the keys the way you want.
It is possible to use reduce() method to create an object with Object.values() to get values from object:
const result = Object.values(products).reduce((a, c) => {
for (const key in c) {
a[key] = a[key] || [];
a[key].push(c[key]);
}
return a;
},{});
An example:
let products = {
Clip:{
today_entry: 0,
today_sold: 0
},
Necklace:
{
today_entry: 0,
today_sold: 2
}
}
const result = Object.values(products).reduce((a, c) => {
for (const key in c) {
a[key] = a[key] || [];
a[key].push(c[key]);
}
return a;
},{});
console.log(result)
const products = {
Clip:{
today_entry: 0,
today_sold: 0
},
Necklace:
{
today_entry: 0,
today_sold: 2
}
}
const result = {};
for(item in products) {
for (action in products[item]) {
result[action]= result[action]||[];
result[action].push(products[item][action])
}
}
console.log(result)
//{ today_entry: [ 0, 0 ], today_sold: [ 0, 2 ] }

Merge Array by one of its value

I want to filter from two different arrays based on following array named = timeArray
["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
first array looks like this .. array1
[
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]
second array looks like this .. array2
[
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23323.25,0,0,"13:35"],
[23296.5,23310,23294.65,23309.8,0,0,"13:30"],
[23308.35,23310,23301,23306.15,0,0,"13:31"],
[23308,23309,23292.5,23299.55,0,0,"13:32"],
[23299.55,23310,23294.15,23310,0,0,"13:33"],
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23324.65,0,0,"13:35"],
[23308,23309,23292.5,23299.55,0,0,"13:36"]
]
Based on Timearray elements, First should check array1 and then array 2 6th element and expected result should look like
[
[23308,23309,23292.5,23299.55,0,0,"13:32"],
[23299.55,23310,23294.15,23310,0,0,"13:33"],
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23324.65,0,0,"13:35"],
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]
I tried something like below
var finalarray = [];
for(var key in timeArray)
{
var timer = timeArray[key];
for(var key in array1)
{
arraytime1 = array1[key][6];
if(timer == arraytime1)
{
finalarray.push(arraytime1);
}
}
for(var key in array2)
{
arraytime2 = array2[key][6];
if(timer == arraytime2)
{
finalarray.push(arraytime2);
}
}
}
But "13:36" is added two times...Also if the element based on timerArray is not present it should be null... also is there any better way to do it ? I feel like it takes much resource to process... Thank you.
You could take an object for keeping the fist array for each time and map the result.
const
timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"],
array1 = [[23323.25, 23323.65, 23313.25, 23315.05, 97, 62, "13:36"], [23315.05, 23315.2, 23314, 23315, 8, 9, "13:37"]],
array2 = [[23310, 23310, 23300, 23300, 0, 0, "13:34"], [23309.75, 23343.1, 23305, 23323.25, 0, 0, "13:35"], [23296.5, 23310, 23294.65, 23309.8, 0, 0, "13:30"], [23308.35, 23310, 23301, 23306.15, 0, 0, "13:31"], [23308, 23309, 23292.5, 23299.55, 0, 0, "13:32"], [23299.55, 23310, 23294.15, 23310, 0, 0, "13:33"], [23310, 23310, 23300, 23300, 0, 0, "13:34"], [23309.75, 23343.1, 23305, 23324.65, 0, 0, "13:35"], [23308, 23309, 23292.5, 23299.55, 0, 0, "13:36"]],
times = [array1, array2].reduce((r, array) => {
array.forEach(a => r[a[6]] ??= a)
return r;
}, {}),
result = timeArray
.sort()
.map(time => times[time] || [0, 0, 0, 0, 0, time]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I believe this is what you want
const base = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
const arrayA = [
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]
const arrayB = [
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23323.25,0,0,"13:35"],
[23296.5,23310,23294.65,23309.8,0,0,"13:30"],
[23308.35,23310,23301,23306.15,0,0,"13:31"],
[23308,23309,23292.5,23299.55,0,0,"13:32"],
[23299.55,23310,23294.15,23310,0,0,"13:33"],
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23324.65,0,0,"13:35"],
[23308,23309,23292.5,23299.55,0,0,"13:36"]
]
// const expectedResult = [
// [23308,23309,23292.5,23299.55,0,0,"13:32"],
// [23299.55,23310,23294.15,23310,0,0,"13:33"],
// [23310,23310,23300,23300,0,0,"13:34"],
// [23309.75,23343.1,23305,23324.65,0,0,"13:35"],
// [23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
// [23315.05,23315.2,23314,23315,8,9,"13:37"]
//]
const res = base.sort().map(time => {
return arrayA.find(subArray => subArray[subArray.length - 1] === time)
|| arrayB.find(subArray => subArray[subArray.length - 1] === time)
|| null
});
console.log(res);
I would modify the arrays in the following format to obtain a much faster access:
{
"13:34": [23310,23310,23300,23300,0,0]
}
Then find the appropriate array for each time item.
Code:
const timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
const array1 = [
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]
const array2 = [
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23323.25,0,0,"13:35"],
[23296.5,23310,23294.65,23309.8,0,0,"13:30"],
[23308.35,23310,23301,23306.15,0,0,"13:31"],
[23308,23309,23292.5,23299.55,0,0,"13:32"],
[23299.55,23310,23294.15,23310,0,0,"13:33"],
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23324.65,0,0,"13:35"],
[23308,23309,23292.5,23299.55,0,0,"13:36"]
]
const values1 = {}
array1.forEach(item => {
values1[item[6]] = item
})
const values2 = {}
array2.forEach(item => {
values2[item[6]] = item
})
const result = []
timeArray.sort().forEach(time => {
let itemResult = [0,0,0,0,0,0, time]
if (values1[time]) {
itemResult = values1[time]
} else if (values2[time]) {
itemResult = values2[time]
}
result.push(itemResult)
})
console.log(result)
in my opinion you can do like this
var finalarray = [];
for(var key in timeArray)
{
var timer = timeArray[key];
for(var key in array2)
{
arraytime2 = array2[key][6];
if(timer == arraytime2)
{
finalarray[arraytime2]=array2[key];
}
}
for(var key in array1)
{
arraytime1 = array1[key][6];
if(timer == arraytime1)
{
finalarray[arraytime1]=array1[key];
}
}
}
It is possible to use Map collection to have O(1) while mapping the items. So we can sort timeArray and then just map() items:
const unique_1 = new Map(array1.map(s => [s[6], s]));
const unique_2 = new Map(array2.map(s => [s[6], s]));
const result = timeArray
.sort()
.map(time => unique_1.get(time) || unique_2.get(time));
An example:
const timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
const array1 = [
[23323.25, 23323.65, 23313.25, 23315.05, 97, 62, "13:36"],
[23315.05, 23315.2, 23314, 23315, 8, 9, "13:37"]
]
const array2 = [
[23310, 23310, 23300, 23300, 0, 0, "13:34"],
[23309.75, 23343.1, 23305, 23323.25, 0, 0, "13:35"],
[23296.5, 23310, 23294.65, 23309.8, 0, 0, "13:30"],
[23308.35, 23310, 23301, 23306.15, 0, 0, "13:31"],
[23308, 23309, 23292.5, 23299.55, 0, 0, "13:32"],
[23299.55, 23310, 23294.15, 23310, 0, 0, "13:33"],
[23310, 23310, 23300, 23300, 0, 0, "13:34"],
[23309.75, 23343.1, 23305, 23324.65, 0, 0, "13:35"],
[23308, 23309, 23292.5, 23299.55, 0, 0, "13:36"]
]
const unique_1 = new Map(array1.map(s => [s[6], s]));
const unique_2 = new Map(array2.map(s => [s[6], s]));
const result = timeArray
.sort()
.map(time => unique_1.get(time) || unique_2.get(time));
console.log(result)

How to get specific keys from object? (JS)

I have this object:
728394 : {
"playersAmount" : 2,
"players" : {
"LRFe9w9MQ6hf1urjAAAB" : {
"nickname" : "spieler1",
"type" : "player1"
},
"nKUDWEd5p_FCBO4sAAAD" : {
"nickname" : "spieler2",
"type" : "player2"
},
"ghdaWSWUdg27sf4sAAAC" : {
"nickname" : "spieler3",
"type" : "spectator"
}
},
"activePlayer" : "LRFe9w9MQ6hf1urjAAAB",
"board" : [
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]
]
}
How do I get everything of the object above except for the k/v pair "board"? is there any other way than just adding every key except the right one?
You can create a copy and then delete the unwanted key:
const copy = { ...original }
delete copy.unwantedProperty
Of course you can instead delete the property on the original if you don't care about mutating it.
(Note: if your environment doesn't support the syntax { ...original }, you can use Object.assign({}, original) instead.)
EDIT: Actually, this answer is even neater.
const { board, ...everythingButBoard } = yourObject
simple answer will be:
const copyObject = Object.assign({}, yourObject) // to make a copy of original variable
delete copyObject['keyToRemove'] // OR delete copyObject.keyToRemove
else if you want to delete from original variable:
delete yourObject['keyToRemove'] // OR delete yourObject.keyToRemove
I think you can create a new object excluding this key using a for...in
object = {
wantedKey: 'wantedValue',
wantedKey2: 'wantedValue2',
wantedKey3: 'wantedValue3',
unwantedKey: 'unwantedValue'
}
const newObject = {}
for (const key in object) {
if (key !== 'unwantedKey') newObject[key] = object[key]
}
console.log(newObject)
for more info about for...in: click here

Easiest way to interate over a complex JSON object via Javascript

I'm consuming JSON data that has a bit of a weird structure for example:
{
"RESULT":
{
"COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
"DATA": [
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
I would like to create some JavaScript that would restructure this data to proper JSON structures so that the "Column" array values become the keys for the "DATA" array's values. So after a JS process is run the data resembles the following:
[
{"ID":7,"name":"Site-A","ENABLED":1,"perms":"1,2","vcenabled":1,"vcvalue":1,"checkenabled":1,"checkvalue":1,"indxenabled":1,"indxvalue":1},
{"ID":15,"name":"Site-B","ENABLED":1,"perms":"1,2","vcenabled":1,"vcvalue":1,"checkenabled":1,"checkvalue":1,"indxenabled":1,"indxvalue":1}
]
What are the JavaScript best practices for accomplishing the JSON restructuring? Could I accomplish this task using a JS framework like JQuery, Foundation JS, ect... ?
Using Underscore, it's a one-liner:
var formatted = _.map(orig.RESULT.DATA, _.partial(_.object, orig.RESULT.COLUMNS));
With plain javascript (less elegant but faster), it would be
var formatted = [],
data = orig.RESULT.DATA,
cols = orig.RESULT.COLUMNS,
l = cols.length;
for (var i=0; i<data.length; i++) {
var d = data[i],
o = {};
for (var j=0; j<l; j++)
o[cols[j]] = d[j];
formatted.push(o);
}
newjson is your new object, j is your json,
code is very fast as it caches the legth and don't uses push.
And as it's pure javascript it's faster than all the libraries.
var j={
"RESULT":{
"COLUMNS":[
"ID",
"name",
"ENABLED",
"perms",
"vcenabled",
"vcvalue",
"checkenabled",
"checkvalue",
"indxenabled",
"indxvalue"
],
"DATA":[
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
var newjson=[],d=j.RESULT.COLUMNS.length;
for(var a=0,b=j.RESULT.DATA.length;a<b;a++){
for(var c=0,tmpObj={};c<d;c++){
tmpObj[j.RESULT.COLUMNS[c]]=j.RESULT.DATA[a][c];
}
newjson[a]=tmpObj;
}
console.log(newjson);
based on Bergi's response u can also use the while-- loop.
var orig={
"RESULT":{
"COLUMNS":[
"ID",
"name",
"ENABLED",
"perms",
"vcenabled",
"vcvalue",
"checkenabled",
"checkvalue",
"indxenabled",
"indxvalue"
],
"DATA":[
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
var formatted = [],
data = orig.RESULT.DATA,
cols = orig.RESULT.COLUMNS,
l = cols.length,
f = data.length;
while (f--) {
var d = data[f],
o = {},
g = l;
while (g--) {
o[cols[g]] = d[g];
}
formatted[f] = o;
}
you can use underscore Array functions for this task
http://underscorejs.org/#arrays
uusing the object function would be helpful
http://underscorejs.org/#object
from the documentation :
_.object(list, [values])
Converts arrays into objects. Pass either a single list of [key, value] pairs, or a list of keys, and a list of values ..the example:
_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
=> {moe: 30, larry: 40, curly: 50}
here is the JSfiddle with the solution
http://jsfiddle.net/rayweb_on/kxR88/1/
and the code looks like this for this specific scenario.
var plain = {
"RESULT":
{
"COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
"DATA": [
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
},
formatted = [];
_.each(plain.RESULT.DATA, function(value) {
var tmp = {};
tmp = _.object(plain.RESULT.COLUMNS,value)
formatted.push(tmp);
});
console.log(formatted);
Try this using underscorejs.
var plain = {
"RESULT":
{
"COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
"DATA": [
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
, formatted = [];
_.each(plain.RESULT.DATA, function(value) {
var tmp = {};
_.each(value, function(parameter, pos) {
tmp[plain.RESULT.COLUMNS[pos]] = parameter;
});
formatted.push(tmp);
});
console.log(formatted);
http://jsfiddle.net/kxR88/
Actually, you could use a combination of Array#map for the array and Array#reduce for the objects with the new properties
var data = { RESULT: { COLUMNS: ["ID", "name", "ENABLED", "perms", "vcenabled", "vcvalue", "checkenabled", "checkvalue", "indxenabled", "indxvalue"], DATA: [[7, "Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0], [15, "Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]] }, ERROR: 0 },
result = data.RESULT.DATA.map(function (a) {
return a.reduce(function (o, d, i) {
o[data.RESULT.COLUMNS[i]] = d;
return o;
}, {});
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
With ES6, you could use Object.assign with spread syntax ....
Object.assign adds properties to the given object and returns this object.
Spread syntax ... takes an array and insert the elements as parameters to the function.
var data = { RESULT: { COLUMNS: ["ID", "name", "ENABLED", "perms", "vcenabled", "vcvalue", "checkenabled", "checkvalue", "indxenabled", "indxvalue"], DATA: [[7, "Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0], [15, "Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]] }, ERROR: 0 },
result = data.RESULT.DATA.map(a =>
Object.assign(...data.RESULT.COLUMNS.map((k, i) => ({ [k]: a[i] }))));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Using JQuery:
function jsonToObj(json){
return jQuery.parseJSON(JSON.stringify(json));
}
For example, after a GET request the server send a complex object
$.get("/Files/-2", function (rxData, status) {
var obj = jsonToObj(rxData);
console.log(obj);
});
Logged in console, can be explored through Chrome's Web Developer (F12), in my case looks like this:
image showing nested levels
By simple JS, your solution would look like this:
var yourObj = {
"RESULT": {
"COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
"DATA": [
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
//Solution
var finalARR = [];
var colLength = yourObj.RESULT.COLUMNS.length;
var dataLength = yourObj.RESULT.DATA.length;
for (var i = 0; i < dataLength; i++) {
var finalJSON = {};
for (var j = 0; j < colLength; j++) {
finalJSON[yourObj.RESULT.COLUMNS[j]] = yourObj.RESULT.DATA[i][j];
}
finalARR[i] = finalJSON;
}
console.log(finalARR);

Categories

Resources