Skip to content Skip to sidebar Skip to footer

Adding A Custom Variable As JSON Attribute

I'm trying to build a JS object with a custom attribute name. Basically I want to create a Schema based on the root element. ('items' if type is array and 'properties' if type is o

Solution 1:

You could do

var helperObj = type.toLowerCase() === "array" ? "items" : "properties";
var newSchema = {
    "title": title,
    "type": type,
};

newSchema[helperObj] = {};

or use if you're using es6:

var helperObj = type.toLowerCase() === "array" ? "items" : "properties";
var newSchema = {
    "title": title,
    "type": type,
    [helperObj] : {}
};

Post a Comment for "Adding A Custom Variable As JSON Attribute"