M
M
Max2018-05-30 16:49:31
JavaScript
Max, 2018-05-30 16:49:31

How to create json string js?

Hello, I'm stupid, there is a view file;

[
    {
        "id": 1,
        "hex": "#D92B2C",
        "line": "Сокольническая",
        "name": "Бульвар Рокоссовского"
    },
    {
        "id": 2,
        "hex": "#D92B2C",
        "line": "Сокольническая",
        "name": "Черкизовская"
    },
    {
        "id": 3,
        "hex": "#D92B2C",
        "line": "Сокольническая",
        "name": "Преображенская площадь"
    },

I need to push the names of the stations into the meaterialize autocomplete
$(document).ready(function(){
    $('input.autocomplete').autocomplete({
      data: {
        "Apple": null,
        "Microsoft": null,
      },
    });
  });

I can't figure out how to make the json string necessary. Tell me who can pzhl
tried to disassemble:
$.getJSON( "/files/metro-file.json", function( data ) {
      var station_name,line_name,line_hex;
      $.each( data, function( key, val ) {
        station_name = val.name;
        line_name = val.line;
        line_hex = val.hex;
            items.push({
                name: station_name+' ('+line_name+')'
            });
      });

    jsonString = JSON.stringify(items);
    console.log(items);
    });

but I’m doing something wrong, it returns emptiness (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
webe, 2018-05-30
@RozMakc

const result = data.reduce((res, item) => ({...res,[item.name]:null}), {})

If I understand the assignment correctly.
if not, then please show the date, which should be the output after converting the array
https://jsfiddle.net/eqgy8x1w/

F
frees2, 2018-05-30
@frees2

var a = {id:1 , .............};
var dAnimal = JSON.stringify(a);
json_name="+dAnimal ; fetch (

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question