JavaScript — Split an array like a string - javascript

Is it possible to split an array into smaller arrays each time a specific value is found? In this case, every time type equals "divider".
Input:
[
{
"type": "type-a",
"name": "Foo"
},
{
"type": "type-b",
"name": "Foo"
},
{
"type": "type-c",
"name": "Foo"
},
{
"type": "divider",
"name": "Foo"
},
{
"type": "type-b",
"name": "Foo"
},
{
"type": "type-b",
"name": "Foo"
},
{
"type": "divider",
"name": "Foo"
},
{
"type": "type-c",
"name": "Foo"
}
]
Desired output:
[
[
{
"type": "type-a",
"name": "Foo"
},
{
"type": "type-b",
"name": "Foo"
},
{
"type": "type-c",
"name": "Foo"
}
],
[
{
"type": "type-b",
"name": "Foo"
},
{
"type": "type-b",
"name": "Foo"
}
],
[
{
"type": "type-c",
"name": "Foo"
}
]
]

This should work
const arr = [{ "type": "type-a", "name": "Foo" }, { "type": "type-b", "name": "Foo" }, { "type": "type-c", "name": "Foo" }, { "type": "divider", "name": "Foo" }, { "type": "type-b", "name": "Foo" }, { "type": "type-b", "name": "Foo" }, { "type": "divider", "name": "Foo" }, { "type": "type-c", "name": "Foo" }];
function divide(container, divider) {
const final = [];
let tmp = new Array();
for (let item of container) {
if (item.type === divider) {
final.push(tmp);
tmp = new Array();
}
item.type !== divider && tmp.push(item)
}
tmp.length > 0 && final.push(tmp)
return final;
}
const res = divide(arr, 'divider');
console.log(res)

You can make use of reduce here as:
const arr = [{ "type": "type-a", "name": "Foo" }, { "type": "type-b", "name": "Foo" }, { "type": "type-c", "name": "Foo" }, { "type": "divider", "name": "Foo" }, { "type": "type-b", "name": "Foo" }, { "type": "type-b", "name": "Foo" }, { "type": "divider", "name": "Foo" }, { "type": "type-c", "name": "Foo" }];
const temp = [];
const result = arr.reduce((acc, curr) => {
if (curr.type === 'divider') {
acc.push([...temp]);
temp.length = 0;
} else {
temp.push(curr);
}
return acc;
}, []);
if (temp.length) result.push([...temp]);
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; }

Try this.
Search each object of the array and till you find the divider one, push all the objects into another array.
If divider is found, then increment the index, ignore this object and continue.
let arr=[{"type":"type-a","name":"Foo"},{"type":"type-b","name":"Foo"},{"type":"type-c","name":"Foo"},{"type":"divider","name":"Foo"},{"type":"type-b","name":"Foo"},{"type":"type-b","name":"Foo"},{"type":"divider","name":"Foo"},{"type":"type-c","name":"Foo"}]
let finalArr = []
let currentIndex = 0;
for (let obj of arr) {
if (obj.type === 'divider') {
currentIndex++;
} else {
(finalArr[currentIndex] = finalArr[currentIndex] || []).push(obj);
}
}
console.log(finalArr)

Here's a curried recursive function:
The first parameter is a predicate function that takes an element and decides whether that element is a delimiter. Then it takes an array and splits it into chunks. Each chunk contains the elements in between (but excluding) a delimiter element.
const splitBy = pred => function loop([x, ...xs], ret = [[]]) {
if (x == null) return ret;
if (pred(x)) ret.push([]);
else ret[ret.length-1].push(x);
return loop(xs, ret);
}
const isDivider = x => x.type == 'divider';
const splitByDivider = splitBy(isDivider);
splitByDivider([ { "type": "type-a" , "name": "Foo" }
, { "type": "type-b" , "name": "Foo" }
, { "type": "type-c" , "name": "Foo" }
, { "type": "divider", "name": "Foo" }
, { "type": "type-b" , "name": "Foo" }
, { "type": "type-b" , "name": "Foo" }
, { "type": "divider", "name": "Foo" }
, { "type": "type-c" , "name": "Foo" }]);
//=> [ [ { "type": "type-a", "name": "Foo" }
//=> , { "type": "type-b", "name": "Foo" }
//=> , { "type": "type-c", "name": "Foo" } ]
//=> , [ { "type": "type-b", "name": "Foo" }
//=> , { "type": "type-b", "name": "Foo" } ]
//=> , [ { "type": "type-c", "name": "Foo" } ]]

Related

How do I destructure this deep nested json objects and map it in JS

I have a nested array like below. There are about 100 de objects in the array. The de objects also have deg[0] array but most likely I will only have the first index. Now the trick is that the de are subset of deg. Which means each deg can have say 10 de. How can I retrieve the deg and there associated de and map it into a new array like:
newArray = [
deg1: [
{de1},
{de2}
],
deg2: [
{de1},
{de2}
]
]
Here is my nested array. I posted four but the list is over a 100.
{
"name": "Report",
"id": "2YYUEZ6I1r9",
"dse1": [
{
"de1": {
"name": "Number",
"id": "HjMOngg3kuy",
"de1-av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg1": [
{
"name": "TB",
"id": "2XJB1JO9qX8"
}
]
}
},
{
"de2": {
"name": "Number of",
"id": "a3dtGETTawy",
"de2-av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg1": [
{
"name": "Secondary",
"id": "w99RWzXHgtw"
}
]
}
},
{
"de1": {
"name": "Number of",
"id": "a3dtGETTawy",
"de1av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg2": [
{
"name": "Secondary",
"id": "w99RWzXHgtw"
}
]
}
},
{
"de2": {
"name": "Number of",
"id": "a3dtGETTawy",
"de2av": [
{
"value": "FHaQMPv9zc7",
"attribute": {
"id": "uwVkIP7PZDt"
}
},
{
"value": "something",
"attribute": {
"id": "FHaQMPv9zc7"
}
}
],
"deg2": [
{
"name": "Tertiary",
"id": "w99RWzXHgtw"
}
]
}
}
]
}
Group array of objects by property (this time a property to be matched by a reg exp) using Array.reduce.
Update: Ignoring missing keys.
var input={name:"Report",id:"2YYUEZ6I1r9",dse1:[{de1:{name:"Number",id:"HjMOngg3kuy","de1-av":[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg1:[{name:"TB",id:"2XJB1JO9qX8"}]}},{de2:{name:"Number of",id:"a3dtGETTawy","de2-av":[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg1:[{name:"Secondary",id:"w99RWzXHgtw"}]}},{de1:{name:"Number of",id:"a3dtGETTawy",de1av:[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg2:[{name:"Secondary",id:"w99RWzXHgtw"}]}},{de2:{name:"Number of",id:"a3dtGETTawy",de2av:[{value:"FHaQMPv9zc7",attribute:{id:"uwVkIP7PZDt"}},{value:"something",attribute:{id:"FHaQMPv9zc7"}}],deg2:[{name:"Tertiary",id:"w99RWzXHgtw"}]}}]}
var reg = new RegExp("^de[0-9]+$");
var reg2 = new RegExp("^deg[0-9]+$");
let obj = input['dse1'].reduce(function(agg, item) {
// do your group by logic below this line
var key = Object.keys(item).find(function(key) {
return key.match(reg) ? key : null;
})
if (key) {
var key2 = Object.keys(item[key]).find(function(key) {
return key.match(reg2) ? key : null;
})
agg[key] = agg[key] || [];
if (key2) {
var to_push = {}
to_push[key2] = item[key][key2]
agg[key].push(to_push)
}
}
// do your group by logic above this line
return agg
}, {});
console.log(obj)
.as-console-wrapper {
max-height: 100% !important;
}

Delete all JSON keys nested having specific name

I want to delete all occurances of keynames like etag,formattedType and metadata in the object using dynamic iteration of whole object
var myjson {
"etag": "%EiIBAgMFBgcICQoLDA0ODxATFBUWGSEiIyQlJicuNTc9Pj9AGgECIdgxQTUREdTBneFMzZz0=",
"names": [{
"unstructuredName": "Natalie Victor",
"displayNameLastFirst": "Victor, Natalie",
"familyName": "Victor",
"displayName": "Natalie Victor",
"givenName": "Natalie",
"metadata": {
"source": {
"id": "c8de0718a7c3458",
"type": "CONTACT"
},
"primary": true
}
}],
"photos": [{
"metadata": {
"primary": true,
"source": {
"id": "c8de0718a7c3458",
"type": "CONTACT"
}
},
"url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAdAA/V8BNOaftJmdYPfvspCwKr2nuTmSEuXTHowCLcDEAEiGQoBThD___________8BGKjbxPr______wE/s100/photo.jpg",
"default": true
}],
"memberships": [{
"metadata": {
"source": {
"type": "CONTACT",
"id": "c8de0718a7c3458"
}
},
"contactGroupMembership": {
"contactGroupId": "6a68e3a408126601",
"contactGroupResourceName": "contactGroups/6a68e3a408126601"
}
}, {
"contactGroupMembership": {
"contactGroupId": "myContacts",
"contactGroupResourceName": "contactGroups/myContacts"
},
"metadata": {
"source": {
"id": "c8de0718a7c3458",
"type": "CONTACT"
}
}
}],
"phoneNumbers": [{
"value": "6767674765",
"formattedType": "Home",
"canonicalForm": "+916767674765",
"metadata": {
"primary": true,
"source": {
"id": "c8de0718a7c3458",
"type": "CONTACT"
}
},
"type": "home"
}],
"emailAddresses": [{
"type": "home",
"formattedType": "Home",
"value": "nati_a_j#hotmail.com",
"metadata": {
"source": {
"id": "c8de0718a7c3458",
"type": "CONTACT"
},
"primary": true
}
}],
"biographies": [{
"contentType": "TEXT_PLAIN",
"value": "Email: ssww#gmail.com\nName.Last: Victor\nName.First: Natalie\nPhone: 6767674765",
"metadata": {
"primary": true,
"source": {
"id": "c8de0718a7c3458",
"type": "CONTACT"
}
}
}],
"resourceName": "people/c904625878430659672"
}
I only know to use delete key by names such as
delete myjson.etag
delete myjson.names[0].metadata
How do I iterate the complete json since some of the json has arrays and nested structures which are not known in advance.
Hence a remove_keys(myjson, ["etag","memberships","formattedType","metadata"]) should render a result
var myjson {
"names": [{
"unstructuredName": "Natalie Victor",
"displayNameLastFirst": "Victor, Natalie",
"familyName": "Victor",
"displayName": "Natalie Victor",
"givenName": "Natalie",
}],
"photos": [{
"url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAdAA/V8BNOaftJmdYPfvspCwKr2nuTmSEuXTHowCLcDEAEiGQoBThD___________8BGKjbxPr______wE/s100/photo.jpg",
"default": true
}],
"phoneNumbers": [{
"value": "6767674765",
"canonicalForm": "+916767674765",
"type": "home"
}],
"emailAddresses": [{
"type": "home",
"value": "nati_a_j#hotmail.com",
}],
"biographies": [{
"contentType": "TEXT_PLAIN",
"value": "Email: ssww#gmail.com\nName.Last: Victor\nName.First: Natalie\nPhone: 6767674765",
}],
"resourceName": "people/c904625878430659672"
}
You need a recursive function for this task:
function filter(obj: any, list: string[]) {
if (obj && typeof obj === 'object') {
for (let item in obj) {
if (list.includes(item)) {
delete obj[item];
} else {
if (Array.isArray(obj[item])) {
for (let el of obj[item]) {
filter(el, list);
}
} else {
filter(obj[item], list);
}
}
}
}
return obj;
}
// example usage:
let a = {b: 5, c: 6, d: { a: 1, b: 2}}
filter(a, ['b']);
console.log(a);
Conversely, it may be faster in some interpreters to rebuild the object, rather than delete keys.
function removeKeys(obj, keys) {
if (Array.isArray(obj))
return obj.map(v => removeKeys(v, keys))
else if (typeof obj == "object" && obj != null) {
const _obj = {}
Object.keys(obj).forEach(k => {
if(!keys.includes(k)) _obj[k] = removeKeys(obj[k], keys)
})
return _obj
}
else
return obj
}
console.log(removeKeys(myjson, ["etag","memberships","formattedType","metadata"]))
this recursive function will solve your problem
var myjson = {
"etag":"%EiIBAgMFBgcICQoLDA0ODxATFBUWGSEiIyQlJicuNTc9Pj9AGgECIdgxQTUREdTBneFMzZz0=",
"names":[
{
"unstructuredName":"Natalie Victor",
"displayNameLastFirst":"Victor, Natalie",
"familyName":"Victor",
"displayName":"Natalie Victor",
"givenName":"Natalie",
"metadata":{
"source":{
"id":"c8de0718a7c3458",
"type":"CONTACT"
},
"primary":true
}
}
],
"photos":[
{
"metadata":{
"primary":true,
"source":{
"id":"c8de0718a7c3458",
"type":"CONTACT"
}
},
"url":"https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAdAA/V8BNOaftJmdYPfvspCwKr2nuTmSEuXTHowCLcDEAEiGQoBThD___________8BGKjbxPr______wE/s100/photo.jpg",
"default":true
}
],
"memberships":[
{
"metadata":{
"source":{
"type":"CONTACT",
"id":"c8de0718a7c3458"
}
},
"contactGroupMembership":{
"contactGroupId":"6a68e3a408126601",
"contactGroupResourceName":"contactGroups/6a68e3a408126601"
}
},
{
"contactGroupMembership":{
"contactGroupId":"myContacts",
"contactGroupResourceName":"contactGroups/myContacts"
},
"metadata":{
"source":{
"id":"c8de0718a7c3458",
"type":"CONTACT"
}
}
}
],
"phoneNumbers":[
{
"value":"6767674765",
"formattedType":"Home",
"canonicalForm":"+916767674765",
"metadata":{
"primary":true,
"source":{
"id":"c8de0718a7c3458",
"type":"CONTACT"
}
},
"type":"home"
}
],
"emailAddresses":[
{
"type":"home",
"formattedType":"Home",
"value":"nati_a_j#hotmail.com",
"metadata":{
"source":{
"id":"c8de0718a7c3458",
"type":"CONTACT"
},
"primary":true
}
}
],
"biographies":[
{
"contentType":"TEXT_PLAIN",
"value":"Email: ssww#gmail.com\nName.Last: Victor\nName.First: Natalie\nPhone: 6767674765",
"metadata":{
"primary":true,
"source":{
"id":"c8de0718a7c3458",
"type":"CONTACT"
}
}
}
],
"resourceName":"people/c904625878430659672"
}
function removeKeys(obj,keys){
if(Array.isArray(obj)){
obj.forEach(innerObj=>{
removeKeys(innerObj,keys)
})
}else{
keys.forEach(k=>{
delete obj[k];
})
Object.keys(obj).forEach(key=>{
if(typeof obj[key]=='object'){
removeKeys(obj[key],keys)
}
})
}
}
removeKeys(myjson, ["etag","memberships","formattedType","metadata"])
console.log(myjson);

convert array into object using underscore only

I am learning underscore now and found a task for it I need help with .. I have an array with object looking like this
[
// ...
{
"type": "presence",
"params": {
"interval": 15,
"foo": "something",
"link": {
"fp_type": "1",
"fp_ext_id": "2"
},
},
{
"type": "bar",
"params": {
"interval": 30,
"foo": "foo",
"link": {
"fp_type": "2",
"fp_ext_id": "3"
},
},
},
// ...
]
The task is using underscore only to convert this array items to an object where the
the key is the items type and the value are its params, i.e.:
{
// ...
"presence": {
"interval": 15,
"foo": "something",
"link": {
"fp_type": "1",
"fp_ext_id": "2"
},
},
"bar": {
"interval": 30,
"foo": "foo",
"link": {
"fp_type": "2",
"fp_ext_id": "3"
},
// ...
}
You can do it this way:
var x = [
{
"type": "presence",
"params": {
"interval": 15,
"foo": "something",
"link": {
"fp_type": "sponsor",
"fp_ext_id": "spotme"
},
},
},
{
"type": "bar",
"params": {
"interval": 30,
"foo": "foo",
"link": {
"fp_type": "2",
"fp_ext_id": "3"
},
},
}
];
var y = _.map(x, function(i) {
let obj = {};
obj[i.type] = i.params;
return obj;
});
//console.log(y);
var result = y.reduce(function(obj,item) {
obj[_.keys(item)[0]] = _.values(item)[0];
return obj;
}, {});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
DEMO

transforming json using recursive function

I'm trying to transform this JSON into array so that I can simply list them as key-value pairs in html. The JSON has nested properties which I want to retain but I want to stringify only those objects that contain "units" and "value". All others should be listed as children items. Please help figure what I'm doing wrong.
Here's the json input:
{
"clusterName": "ml6.engrlab.com-cluster",
"statusProperties": {
"loadProperties": {
"loadDetail": {
"restoreWriteLoad": {
"value": 0,
"units": "sec/sec"
},
},
"totalLoad": {
"value": 0.0825921967625618,
"units": "sec/sec"
}
},
"secure": {
"value": false,
"units": "bool"
},
"statusDetail": {
"licenseKeyOption": [
{
"value": "conversion",
"units": "enum"
},
{
"value": "failover",
"units": "enum"
}
],
"connectPort": 7999,
"softwareVersion": {
"value": 9000100,
"units": "quantity"
}
},
"rateProperties": {
"rateDetail": {
"largeReadRate": {
"value": 0,
"units": "MB/sec"
}
},
"totalRate": {
"value": 67.2446365356445,
"units": "MB/sec"
}
},
"online": {
"value": true,
"units": "bool"
},
"cacheProperties": {
"cacheDetail": {
"tripleCachePartitions": {
"tripleCachePartition": [
{
"partitionSize": 768,
"partitionBusy": 0,
"partitionUsed": 0,
"partitionFree": 100
}
]
}
}
}
}
}
my code
function isNested(obj) {
if(!obj) return false;
let propArry = Object.keys(obj)
for(let i=0; i<propArry.length; i++){
if(obj[propArry[0]].constructor.name === 'Object') return true
}
return false;
}
function getKeyValueAsChildren(obj) {
let vals = [];
for(let key in obj) {
if(obj.hasOwnProperty(key)){
vals.push({key:key, value: obj[key]})
}
}
return vals
}
function valueAsString(obj) {
if(typeof obj !== 'object') return obj;
return `${obj['value']} ${obj['units']}`
}
function getChildren(key, obj) {
if(Object.keys(obj).sort().toString() === "units,value"){
return {key: key, value: valueAsString(obj)}
} else {
return {key: key, children: getKeyValueAsChildren(obj) }
}
}
function getValues(properties, values = []) {
for(let key in properties) {
if(properties.hasOwnProperty(key)) {
let value = properties[key]
if(typeof value !== 'object') {
values.push({key: key, value: value})
} else if(Array.isArray(value)){
let children = []
value.map(v => {
children.push(getChildren(key, v))
})
values.push({key: `${key}List`, children: children})
}
else if(value.constructor.name === 'Object' && isNested(value)){
// I THINK THE MAIN PROBLEM IS HERE
let keys = Object.keys(value)
let children = [];
keys.forEach(key => {
children.push({key: key, children: getValues(value[key])})
})
values.push({key: key, children: children})
}
else {
values.push(getChildren(key, value))
}
}
}
return values
}
getValues(hostProperties)
this returns
[
{
"key": "clusterName",
"value": "ml6.engrlab.com-cluster"
},
{
"key": "statusProperties",
"children": [
{
"key": "loadProperties",
"children": [
{
"key": "loadDetail",
"children": [
{
"key": "restoreWriteLoad",
"children": [
{
"key": "value",
"value": 0
},
{
"key": "units",
"value": "sec/sec"
}
]
}
]
},
{
"key": "totalLoad",
"value": "0.0825921967625618 sec/sec"
}
]
},
{
"key": "secure",
"children": [
{
"key": "value",
"value": false
},
{
"key": "units",
"value": "bool"
}
]
},
{
"key": "statusDetail",
"children": [
{
"key": "licenseKeyOptionList",
"children": [
{
"key": "licenseKeyOption",
"value": "conversion enum"
},
{
"key": "licenseKeyOption",
"value": "failover enum"
}
]
},
{
"key": "connectPort",
"value": 7999
},
]
},
{
"key": "rateProperties",
"children": [
{
"key": "rateDetail",
"children": [
{
"key": "largeReadRate",
"children": [
{
"key": "value",
"value": 0
},
{
"key": "units",
"value": "MB/sec"
}
]
}
]
},
{
"key": "totalRate",
"value": "67.2446365356445 MB/sec"
}
]
},
{
"key": "online",
"children": [
{
"key": "value",
"value": true
},
{
"key": "units",
"value": "bool"
}
]
},
{
"key": "cacheProperties",
"children": [
{
"key": "cacheDetail",
"children": [
{
"key": "tripleCachePartitions",
"children": [
{
"key": "tripleCachePartitionList",
"children": [
{
"key": "tripleCachePartition",
"children": [
{
"key": "partitionSize",
"value": 768
},
{
"key": "partitionBusy",
"value": 0
},
{
"key": "partitionUsed",
"value": 0
},
{
"key": "partitionFree",
"value": 100
}
]
}
]
}
]
}
]
}
]
}
]
}
]
but I need this
[
{
"key": "clusterName",
"value": "ml6.engrlab.com-cluster"
},
{
"key": "statusProperties",
"children": [
{
"key": "loadProperties",
"children": [
{
"key": "loadDetail",
"children": [
{
"key": "restoreWriteLoad",
"value": "0 sec/sec"
}
]
},
{
"key": "totalLoad",
"value": "0.0825921967625618 sec/sec"
}
]
},
{
"key": "secure",
"value": "false bool"
},
{
"key": "statusDetail",
"children": [
{
"key": "licenseKeyOptionList",
"children": [
{
"key": "licenseKeyOption",
"value": "conversion enum"
},
{
"key": "licenseKeyOption",
"value": "failover enum"
}
]
},
{
"key": "connectPort",
"value": 7999
}
]
},
{
"key": "rateProperties",
"children": [
{
"key": "rateDetail",
"children": [
{
"key": "largeReadRate",
"value": "0 MB/sec"
}
]
},
{
"key": "totalRate",
"value": "67.2446365356445 MB/sec"
}
]
},
{
"key": "online",
"value": "true bool"
},
{
"key": "cacheProperties",
"children": [
{
"key": "cacheDetail",
"children": [
{
"key": "tripleCachePartitions",
"children": [
{
"key": "tripleCachePartitionList",
"children": [
{
"key": "tripleCachePartition",
"children": [
{
"key": "partitionSize",
"value": 768
},
{
"key": "partitionBusy",
"value": 0
},
{
"key": "partitionUsed",
"value": 0
},
{
"key": "partitionFree",
"value": 100
}
]
}
]
}
]
}
]
}
]
}
]
}
]
You might have to run more tests, as what you require seems quite arbitrary, but I believe this function does what you are looking for. It works in a similar way to yours, by simply recursing the tree, and checking for object types, and if the special case is matched.
function toKeyValue(obj) {
return Object.keys(obj).map(k => {
var value = obj[k]
var key = k
var valueName = 'children'
if(Array.isArray(value)){
key = k + 'List'
value = value.map(toKeyValue)
}else if(value !== null && typeof value === 'object'){
// check for special case
if(Object.keys(value).sort().toString() === "units,value"){
value = value.value + ' ' + value.units
valueName = 'value'
}else{
value = toKeyValue(value)
}
}else{
valueName = 'value'
}
return {
key: key,
[valueName]: value
}
})
}
Fiddle here

Building new JSON from existing one

I want to build an new JSON from existing one. The source has sections and rubrics that I no longer need for a listing. The new object called 'items' should have an array of the items.
The final JSON should be sorted by attribute 'name' and look like
{
"items": [
{
"id": 10000006,
"name": "Boah"
},
{
"id": 10000013,
"name": "Gut"
},
{
"id": 10000003,
"name": "Ipsum"
},
{
"id": 10000001,
"name": "Lorem"
},
{
"id": 10000005,
"name": "Lorum"
},
{
"id": 10000004,
"name": "Name"
},
{
"id": 10000002,
"name": "Stet"
}
]
}
For building the new JSON I get this source:
{
"sections": [
{
"name": "FooBar",
"rubrics": [
{
"name": "Foo",
"items": [
{
"id": 10000001,
"name": "Lorem"
},
{
"id": 10000002,
"name": "Stet"
},
{
"id": 10000003,
"name": "Ipsum"
}
]
},
{
"name": "Bar",
"items": [
{
"id": 10000004,
"name": "Name"
},
{
"id": 10000005,
"name": "Lorum"
},
{
"id": 10000006,
"name": "Boah"
}
]
}
]
},
{
"name": "BlahBloob",
"rubrics": [
{
"name": "Bla",
"items": [
{
"id": 10000013,
"name": "Gut"
}
]
},
{
"name": "Bloob",
"items": [
{
"id": 10000014,
"name": "Name"
},
{
"id": 10000015,
"name": "Lorem"
}
]
}
]
}
]
}
What do you think? How can I do this with plain JavaScript or maybe TypeScript?
Thanks for reading and have time for my question. And thanks for reply in advance.
Here you go. You just need to iterate over each rubric of each section of your source to get the items. At the end, sort your list of items by items, and you're done.
This example uses ES6 syntax, but it's easy to convert it to ES5 if needed.
function extractItems(source) {
const items = [];
for (const section of source.sections) {
for (const rubric of section.rubrics) {
items.push(...rubric.items);
}
}
items.sort((a, b) => a.name.localeCompare(b.name));
return { items };
}
A more functional approach use map and reduce to pick the rubrics and merge them.
data.sections
.map(section => section.rubrics) // get rubrics
.reduce((a, b) => a.concat(b)) // merge rubrics
.map(rubric => rubric.items) // get items from each rubric
.reduce((a, b) => a.concat(b)) // merge items
.sort((a, b) => a.name.localeCompare(b.name)); // sort
function(oldObj) {
var newObj = {
"items": []
};
oldObj.sections.forEach(function(section) {
section.rubrics.forEach(function(rubric) {
rubric.items.forEach(function(item) {
newObj.items.push(item);
});
});
});
newObj.items = newObj.items.sort(function(a, b) {
if (a.name < b.name) { return -1; }
if (a.name > b.name) { return 1; }
return 0;
});
return newObj;
}
And simply use JSON.parse() and JSON.stringify() to convert JSON to and from objects.
It might help you
var data ={
"sections": [
{
"name": "FooBar",
"rubrics": [{"name": "Foo", "items": [{"id": 10000001,"name": "Lorem"}, {"id": 10000002,"name": "Stet"}, {"id": 10000003,"name": "Ipsum"}]
}, {
"name": "Bar",
"items": [{
"id": 10000004,
"name": "Name"
}, {
"id": 10000005,
"name": "Lorum"
}, {
"id": 10000006,
"name": "Boah"
}]
}]
}, {
"name": "BlahBloob",
"rubrics": [{
"name": "Bla",
"items": [{
"id": 10000013,
"name": "Gut"
}]
}, {
"name": "Bloob",
"items": [{
"id": 10000014,
"name": "Name"
}, {
"id": 10000015,
"name": "Lorem"
}]
}]
}]
};
var itemObj = {};
var itemArr = [];
var sections = data.sections;
for(var i=0;i<sections.length;i++)
{
for(var j=0;j<sections[i].rubrics.length;j++){
for(var k=0;k<sections[i].rubrics[j].items.length;k++){
var itemObj;
itemObj['id'] = sections[i].rubrics[j].items[k].id;
itemObj['name'] = sections[i].rubrics[j].items[k].name;
itemArr.push(itemObj);
}
}
}
var finalObj = {"items":itemArr};
console.log(finalObj);
JSFiddle

Categories

Resources