0

I have an object array that looks like the following example. I want to create a duplicate of it without the data values.

var fruits = ['Apple', 'Banana'];
var now = new Date();
var x = [{ 'fruits': fruits },
         { 'producer': 'farmer john', 'address': '123 old patch road', 'zip': 90291 },
         27, 'hm',
         { 'asked': now }
        ]

Now assuming there is some JavaScript function, the new object structure would be copied into the following:

var theStructure = [{ 'fruits': ['',''] },
                    { 'producer': '', 'address': '', 'zip': NaN },
                    NaN, '', 
                    {'asked': NaN }]

I have read the posting at How to copy an object's structure (but not the data) but don't see how to make it apply in my case. Perhaps the solution is a long hand parsing of the original object with case statements to handle each data type.

My application is in AngularJS incase there is a simple way to provide a solution.

2
  • Maybe lodash#cloneDeepWith can help you with that. lodash.com/docs/4.17.11#cloneDeepWith Commented Nov 28, 2018 at 16:29
  • The problem I am facing with trying to come up with a recursive function that does the job is the fact that javascript considers arrays and objects as objects, but in the solution you want, if it's an array you want to not show the keys (0,1,2, etc..) but if it's an object you want them ({fruits: ''}, etc..). Edit: working with instanceof so see if I can come up with a solution. Commented Nov 28, 2018 at 17:10

2 Answers 2

3

It's really just a matter of of recursing until you end up at a type of object or value that want to obliterate. In you example you need to distinguish between Numbers, Dates, and Strings. It's a little tricky because new Date returns an object, but you want it to become NaN.

Nevertheless, maybe this will be enough to get started:

var fruits = ['Apple', 'Banana'];
var now = new Date();
var x = [{ 'fruits': fruits }, { 'producer': 'farmer john', 'address': '123 old patch road', 'zip': 90291 }, 27, 'hm', { 'asked': now }]


function stripValues(obj){
  if(Array.isArray(obj)) return obj.map(stripValues)
  if (typeof obj == 'object') {
    return (obj instanceof Date) 
      ?  NaN
      : Object.entries(obj).reduce((newObj, [key, value]) => {
          newObj[key] = stripValues(value)
          return newObj
    }, {})
  }
  return isNaN(obj) ? '' : NaN
}
console.log(stripValues(x))

Sign up to request clarification or add additional context in comments.

3 Comments

for the use case specified, I came up with a solution. Added it below, let me know if you see any bugs.
This is very clever and seems to work. I'm still rapping my head around how you loop through all of the nested properties so perfectly.
I understand @HarveyMushman — I often find recursive functions to be a little mind-bending.
0

This code works for me.

var fruits = ["Apple", "Banana"];
var now = new Date();
var x = [
  { fruits: fruits },
  { producer: "farmer john", address: "123 old patch road", zip: 90291 },
  27,
  "hm",
  { asked: now }
];

// var theStructure = [
//   { fruits: ["", ""] },
//   { producer: "", address: "", zip: NaN },
//   NaN,
//   "",
//   { asked: NaN }
// ];


function makeStructureRecursively(obj) {
  let arr;
  arr = obj instanceof Array;
  if (arr) return mapArrayElements(obj);
  arr = obj instanceof Date;
  if(arr) return NaN;
  arr = obj instanceof Object;
  if (arr) return mapObjectValues(obj);
  if (typeof obj === "number") return NaN;
  if (typeof obj === "string") return "";
  return undefined;
}

function mapArrayElements(arr) {
  return arr.map(el => makeStructureRecursively(el));
}

function mapObjectValues(obj) {
  return Object.keys(obj).map(key => ({
    [key]: makeStructureRecursively(obj[key])
  })).reduce((a,b)=>({...a,...b}),{});
}

var theStructure = makeStructureRecursively(x);

Of course, as the complexity increases, you have to modify this code. For example, I haven't handled boolean, symbol, undefined, etc.

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.