Modify javascript object

1

I am trying to modify an object so that it fits me with the following format:

["Antena" : "red", "Antena FM" : "red", "Antena TV" : "red", "Antena GSM" : "red", "Antena LTE" : "red", "Antena WCDMA" : "red", "Arquitectura de transmisión" : "red", "Antena" : "red"]

But the only thing I get is something like this:

["Antena: red", "Antena FM: red", "Antena TV: red", "Antena GSM: red", "Antena LTE: red", "Antena WCDMA: red", "Arquitectura de transmisión: red", "Antena: red"]

The code that generates it is:

var names = filtrados.map(function(x) { return x['Nombre'] + ": red"});

How do I make it look the way I want?

    
asked by ch3k0 18.05.2016 в 19:02
source

3 answers

1

It is not entirely clear to me if what you are trying to achieve is a single object with the properties defined in the filtered variable, all of them with "red" :

$(function(){
  var filtrados = [
    {Nombre: "Antena"},
    {Nombre: "Antena TV"},
    {Nombre: "Antena GSM"},
    {Nombre: "Antena LTE"},
    {Nombre: "Antena WCDMA"},
    {Nombre: "Arquitectura de trasmisión"},
    {Nombre: "Antena"}
  ];

  var names = {};
  filtrados.forEach(function(x) { names[x["Nombre"]] = "red"; });
  $(".resultado").text(JSON.stringify(names));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="resultado"></span>
    
answered by 18.05.2016 / 19:17
source
0

An array of objects has this form, look at the keys:

[ {"Antena" : "red"}, {"Antena FM" : "red"} ... ]

Then in your code:

var names = filtrados.map(function(x) { return { [x['Nombre']]: "red" }; });

You have to return an object for each item, but the detail is that the property name of each object is variable, which is why [] is used around x['Nombre'] .

If what you want is a single object where each property comes from the array as a dictionary then:

var names = filtrados.reduce(function(prev, current) {
    prev[current.Nombre] = 'red'; 
    return prev; 
}, { });
    
answered by 18.05.2016 в 19:12
0

you can try like this:

var customList = [];
$.each(filtrados, function(i, item) {
  customList[i] = item;
});

with that you will get what you need.

Greetings

    
answered by 18.05.2016 в 19:30