0

I am trying to create an object based on the contents of another object. I have an object that contains the following key/values:

var inputObject = {
    "color": [
        "black",
        "red",
        "green"
    ],
    "bottom": [
        "A",
        "B",
        "C",
        "D",
    ],
    "top": [
        "L",
        "M"
    ]
    }

I would like to modify this object so that it creates a dynamic number of keys. Here is my intended output:

var outputObject = {

    "blackBottom": [
        "A",
        "B",
        "C",
        "D",
    ],
    "redBottom": [
        "A",
        "B",
        "C",
        "D",
    ],
    "greenBottom": [
        "A",
        "B",
        "C",
        "D",
    ],
    "blackTop": [
        "L",
        "M"
    ],
    "redTop": [
        "L",
        "M"
    ],
    "greenTop": [
        "L",
        "M"
    ]
}

Where the following conditions are met:

  • Should there be no colors inside the "color" key, or the "color" key not exist, then create all 4 "color" + "Bottom" and "Top"
  • Should there be only one color inside the "color" object, then only create that "color" + "Bottom" and "Top"
  • Should there be only no entries in "Bottom", then only create the "color" + "Bottom"

How do I create the outputObject?

1
  • What have you tried? Where is your problem. Just a reminder, people here really don't like doing your homework... Commented Aug 23, 2017 at 14:38

2 Answers 2

3

Iterate over each color and fill resulting object using reduce

// The ES6 version, with the flexibility requested

const generate = ({color: colors, bottom, top}) => 
   colors.reduce(
     (acc, color) => Object.assign(acc, 
       bottom && {[`${color}Bottom`]: [...bottom]},
       top && {[`${color}Top`]: [...top]}
       ), 
     {})

// The ES5 version

function generateOldGoodES5(input) {
   // extract values from input w/o destructuring
   var colors = input.color,
       bottom = input.bottom,
       top = input.top;

   return colors.reduce(function(out, color){
     // assign corresponding properties
     out[color + 'Bottom'] = bottom.slice(); // copy array
     out[color + 'Top'] = top.slice(); // copy array

     return out
   }, {});

}

var inputObject = {
    "color": [
        "black",
        "red",
        "green"
    ],
    "bottom": [
        "A",
        "B",
        "C",
        "D",
    ],
    "top": [
        "L",
        "M"
    ]
    }
    
console.log("ES6 Output")
console.log(generate(inputObject))

console.log("ES5 Output")
console.log(generateOldGoodES5(inputObject))

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

5 Comments

Nice use of ES6! However, i dont think the OP will understand without an explanation...
@Jonasw ES5 version added with some comments.
I like the ES6 version, however it only works when all "color", "top", and "bottom" exist. Is there any way that the solution works when either "top" or "bottom" do not exist?
@black_sheep07 in the func parameters, you can use bottom = [], top = [] to make it work.
@black_sheep07 You could do it like this Object.assign(acc, top && {[${color}Top]: [...top]}, bottom &&/*same here*/}
0

You can add informations dynamically to your outputObject dynamically just like this:

var outputObject = {
   // unconditional things
   exampleUnconditional: [
      "someValue"
   ]
};

if(inputObject. /* your condition here */) {
    outputObject.exampleConditional = [
        "someContent"
    ] 
};

And so as result, outputObject will be like:

{
    exampleUnconditional: [
       "someValue"
    ],
    exampleConditional: [
        "someContent"
    ]
}

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.