0

I must create a Json object that represent a Sandwich configuration (bread, meat, sauces etc...) from different arrays that contains the ingredients for each category (1 array for bread, 1 for meat etc...) like this {name: 'x', price: 'y'}

The categories are defined but each array can have a different number of elements.

It's possible to obtain, starting from these arrays, something like that by code? I need this to make a recap of my sandwich in the cart page after the user choose all the ingredients. (each cat_1 it's a type of ingredient).

sandwich{
"cat_1": {[
        {
            "name": "x",
            "price": "x"
        }
    ]
},
"cat_2": {[
        {
            "name": "x",
            "price": "x"
        }
    ]

},
"cat_3": {[
    {
        "name": "x",
            "price": "x"
    },
    {
        "name": "x",
        "price": "x"
    },
    {
        "name": "x",
        "price": "x"
    }
    ]

},
"cat_4": {[
        {
            "name": "x",
            "price": "x"
        },
        {
            "name": "x",
            "price": "x"
        }
    ]
},
"cat_5": {[
        {
            "name": "x",
            "price": "x"
        },
        {
            "name": "x",
            "price": "x"
        },
        {
            "name": "x",
            "price": "x"
        }
    ]
}      

}

I don't post any code beacuse I don't know what can help.

Actually, I push all my arrays into a big array and I have something like that, but it's a lot different from what I want achieve.

[{
    "name": "Montanaro",
    "price": "5.00"
}, {
    "name": "Manzo",
    "price": "5.00"
}, {
    "name": "Fossa",
    "price": "1.00"
}, {
    "name": "Caciotta",
    "price": "1.00"
}, {
    "name": "Guacamole",
    "price": "0.50"
}, {
    "name": "Olive ascolane",
    "price": "1.00"
}, {
    "name": "Mozzarelline fritte",
    "price": "0.50"
}, {
    "name": "Onion Rings",
    "price": "1.00"
}]

I'm not a json expert so I don't know where to start...

Thank you to everyone that could help me.

Have a good day

3
  • 1
    what is your input and expected output? Commented Nov 5, 2018 at 17:32
  • @JEricaM "Json object" is a misnomer. JSON stands for Javascript Object notation. It is a way to represent a Javascript Object as text. You want to create a javascript object, not notate an existing Javascript Object. Commented Nov 5, 2018 at 17:51
  • Seems like the first object (with categories) is what you want. But you have not provided any information about the input that must be transformed to that object. Where are cat_1, cat_2 etc. coming from? Commented Nov 5, 2018 at 17:57

1 Answer 1

1

Have you tried to split the ingredients and add an element from the list for each sandwich?

let breads = [{name: "bread1", price: 1.00}, {name: "bread2", price: 2.00}, {name: "bread3", price: 3.00}];
let meats  = [{name: "meat1" , price: 1.00}, {name: "meat2" , price: 2.00}, {name: "meat3" , price: 3.00}];
let sauces = [{name: "sauce1", price: 1.00}, {name: "sauce2", price: 2.00}, {name: "sauce3", price: 3.00}];


let s1 = {
  bread : breads[Math.floor(Math.random()*breads.length)],
  meats : meats[Math.floor(Math.random()*meats.length)],
  sauces : sauces[Math.floor(Math.random()*sauces.length)]
}
let sandwich = {s1};

console.log(sandwich);

Here is an example: https://codepen.io/WildLlama/pen/VVLYmL?editors=0011

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

1 Comment

this is what I want to achieve :D Thank you so much!

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.