Replace values in a JSON array

0

First of all thank you very much for helping me! :) I'm starting in the world of programming, and there's something I'm not able to solve / understand. I have a website in development that uses Template7, where I basically have an array in JSON where its values are replaced in an HTML page.

Example:

template7Data: {
    datos: [
        {
            id: 01,
            nombre: 'Marcos',
            color: 'azul',
        },
        {
            id: 02,
            nombre: 'Esteban',
            color: 'colorado',
        },
    ],
}

On the website I use {{nombre}} to add the names and {{color}} to add colors, etc.

Example raw HTML code:

<p>Bienvenido {{nombre}}. ¡A mi también me gusta el color {{color}}!</p>

Final HTML code (when loading the web):

<p>Bienvenido Esteban. ¡A mi también me gusta el color colorado!</p>

Everything works wonders, but my question is this:

How can I do to replace values within the JSON, before I replace them on the web?

For example, if suddenly I want to replace the "red" with "red" so that at the end it is read:

<p>Bienvenido Esteban. ¡A mi también me gusta el color rojo!</p>

It is worth clarifying that I have a much more extensive array, and I would like to change ALL the values that are "colored" for example, without discriminating where they are.

I hope it was as clear as possible. The truth is that I am very lost. Thank you very much!

    
asked by Mats 16.05.2018 в 15:59
source

1 answer

0

Maybe something like this will help you:

function reempalzar(colorBuscado,nuevoColor){
    template7Data.datos.forEach(function (item){
        if(item.color==colorBuscado){
            item.color=nuevoColor;
        }
    });
}
    
answered by 30.05.2018 в 23:35