1

i am trying to flatten the following JSON file into an array:

{
  "@context": "http://schema.org",
  "@type": "EventReservation",
  "reservationNumber": "IO12345",
  "underName": "John Smith",
  "reservationFor": [{
    "@type": "Event",
    "name": "Google I/O 2013",
    "startDate": "2013-05-15T08:30:00-08:00",
    "location": "Moscone Center, 800 Howard St., San Francisco, CA 94103"
  }, {
    "@type": "Event",
    "name": "Apple I/O 2013",
    "startDate": "2016-05-15T08:30:00-08:00",
    "location": "Evergllades Center, 800 Howard St., San Francisco, CA 94103"
  }, {
    "@type": "Event",
    "name": "Apple I/O 2013",
    "startDate": "2016-05-15T08:30:00-08:00",
    "location": "Evergllades Center, 800 Howard St., San Francisco, CA 94103"
  }]
}

I want to produce checkboxes with this array. The problem is I get array indices like this: @[email protected]@Event.location

which should rather look like:

@[email protected]@Event.location

And I don't understand why this is the case. My code looks like this:

function toArray(obj, namespace) {
  var result = [];
  var array = obj instanceof Array;
  namespace +=  !array ? "@" + obj["@type"] + "." : "@Array.";
  for (var prop in obj) {    
    if(prop == "@context" || prop == "@type") continue;    
    var value = obj[prop];
    if (typeof value === 'object') {  
      namespace += prop;     
      toArray(value, namespace).forEach(function(element) {
        result.push(element);
      });
    }
    else {
      result.push(namespace + prop);
    }
  }
  return result;
}

I am trying around for hours now and i can't figure out a solution so i would appreciate your help a lot. :D

2 Answers 2

1

So it looks like you are appending to your namespace variable every loop. It doesn't look quite right. Try this instead:

function toArray(obj, namespace) {
  var result = [];
  var array = obj instanceof Array;
  namespace +=  !array ? "@" + obj["@type"] + "." : "@Array.";
  for (var prop in obj) {    
    if(prop == "@context" || prop == "@type") continue;    
    var value = obj[prop];
    if (typeof value === 'object') {  
      toArray(value, namespace + prop).forEach(function(element) {
        result.push(element);
      });
    }
    else {
     result.push(namespace + prop);
    }
  }
  return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!! You have been a great help.
0

All of these methods work - the times are almost the same.

function testFlatten() {  

  let arr2D = [ ... giant array here . . . ];


  let arr3D = [ ... giant array here . . . ];
  
  let startTime, endTime, diffTime;
  
  startTime = new Date().getTime();
  console.log("-------------------- flat startTime: " + startTime );
  
  flatWithConcat (arr2D);
  flatWithConcat (arr3D);
  
  endTime = new Date().getTime();
  console.log("endTime: " + endTime );  
  diffTime = (endTime - startTime)/1000;
  console.log("flatWithConcat diffTime: " + diffTime );  
    
  startTime = new Date().getTime();
  console.log("startTime: " + startTime );
  
  flatWithFlat  (arr2D);
  flatWithFlat  (arr3D);
  
  endTime = new Date().getTime();
  console.log("endTime: " + endTime );  
  diffTime = (endTime - startTime)/1000;
  console.log("flatWithFlat  diffTime: " + diffTime ); 
    
  startTime = new Date().getTime();
  console.log("----------------- reduce startTime: " + startTime );

 flatWithReduce  (arr2D);
 flatWithReduce  (arr3D);
 
  endTime = new Date().getTime();
  console.log("endTime: " + endTime );  
  diffTime = (endTime - startTime)/1000;
  console.log("flatWithReduceRight  diffTime: " + diffTime );   
//   
  startTime = new Date().getTime();
  console.log("----------------- concat startTime: " + startTime );

 flatRedArrowConcat  (arr2D);
 flatRedArrowConcat  (arr3D);
 
  endTime = new Date().getTime();
  console.log("endTime: " + endTime );  
  diffTime = (endTime - startTime)/1000;
  console.log("flatWithReduceRight  diffTime: " + diffTime );   
// 
//    
  startTime = new Date().getTime();
  console.log("----------------- reduce rt startTime: " + startTime );

 flatWithReduceRight  (arr2D);
 flatWithReduceRight  (arr3D);
 
  endTime = new Date().getTime();
  console.log("endTime: " + endTime );  
  diffTime = (endTime - startTime)/1000;
  console.log("flatWithReduceRight  diffTime: " + diffTime );   
    
  startTime = new Date().getTime();
  console.log("----------------- map startTime: " + startTime );
  
  flatWithMap  (arr2D);
  flatWithMap  (arr3D);
  
  endTime = new Date().getTime();
  console.log("endTime: " + endTime );  
  diffTime = (endTime - startTime)/1000;
  console.log("flatWithMap  diffTime: " + diffTime );   
  
}

function flatWithConcat (inArray)  { 
  let newArray = [].concat.apply([], inArray);
//  console.log('Reduce right: ' + JSON.stringify(newArray));
}

function flatWithFlat (inArray)  { 
  let newArray = inArray.flat();
//  console.log('Reduce right: ' + JSON.stringify(newArray));
}

function flatWithReduce (inArray)  {
  let newArray = inArray.reduce(
    function (accumulator, currentValue) {
      return accumulator.concat(currentValue);
    });
//  console.log('flatWithReduce: ' + JSON.stringify(newArray));
}

function flatRedArrowConcat (inArray)  {
  let newArray = inArray.reduce(
    (acc, current) => acc.concat(current));
//  console.log('flatRedArrowConcat: ' + JSON.stringify(newArray));
}

function flatWithReduceRight (inArray)  {
  let newArray = inArray.reduceRight(
    function (accumulator, currentValue) {
      return accumulator.concat(currentValue);
    });
//  console.log('flatWithReduceRight: ' + JSON.stringify(newArray));
}

function flatWithMap (inArray)  {
  let newArray = inArray.map(
    function (e) {
      return e[0];
    });
//  console.log('map: ' + JSON.stringify(newArray)); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.