1

I have javascript objects like:

{["186,2017"]}

I would like to only have:

186, 2017

I tried (thougth i had a json):

console.log(JSON.stringify(data));

But that is what gives me the json and not the string values only,

This is how I get the whoel thing:

$("#years").slider({
      tooltip: 'always',
      tooltip_position:'left',
      formatter: function(value) {
        currentRange = value instanceof Array ? value.join('-'): value;
        return 'Years: ' + (value instanceof Array ? value.join('-'): value);
        console.log(value instanceof Array ? value.join(', '): value);
      }
    });

    getData = function() {
      var data = {
          years: $('#years').slider().val().split(', ')
      }  
      console.log(JSON.stringify(data));
    }

    $("#years").on("slide", function(slideEvt) {
      getData();
    });
11
  • 1
    {["186,2017"]} is not valid JSON - if the JSON is ["186,2017"] ... JSON.parse(json).map(Number) - though, that would result in [186,2017] ... because you still have an array ... so, perhaps ... JSON.parse(json).join(", ") - in the above , json is the JSON string that "you have" ... if what you have is an array, data .. then you just want data.join(', ') Commented May 30, 2018 at 1:55
  • @JaromandaX I have updated the question, that format is what I get when I use stringify Commented May 30, 2018 at 1:57
  • right, so you don't have JSON at all you have data = {years: ["186", "2017"]} Commented May 30, 2018 at 1:59
  • @JaromandaX actually yes... thought that was a json tho Commented May 30, 2018 at 1:59
  • you still don't have JSON ... JSON would be a string Commented May 30, 2018 at 2:00

2 Answers 2

1

let obj = { years: ["186", "2017"] }

console.log(...obj.years);

You can try to spread it as above

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

1 Comment

but that won't split the elements by ", " just "," :p
0

I gather the object you are dealing with is a variable called data with the content {years: ["186", "2017"]}

to "get" the value "186, 2017" from that data

data.years.join(", ");

Comments

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.