1

So this is my question (maybe stupid), is there any possible to do this:

 var data {
     "label" : value,
     "sets" : [             
         for (var i=0; i < item.length; i++)
         {
             somedata: "data"   
         }
     ]  
 }

to reach result:

 var data {
     "label" : value,
     "sets" : [  
         {
            somedata: "data1"
         },
         {
            somedata: "data2"
         }
     ]  
 }

Much thx for help.

7
  • 1
    No. JSON contains data, not code. Commented Aug 14, 2014 at 12:06
  • No. That's not possible. Commented Aug 14, 2014 at 12:06
  • 3
    The exact result you want is really unclear. Commented Aug 14, 2014 at 12:06
  • 1
    I must say it's a nice abstract idea. Commented Aug 14, 2014 at 12:07
  • 1
    Can you confirm you don't really mean JSON (that is a string used for data exchange) but a literal object ? "JSON object" has no meaning. Commented Aug 14, 2014 at 12:12

6 Answers 6

2

As jimm101 has pointed out, you are not working with JSON, that's just JavaScript (the var in there proves it) . If you want to calculate a value inside a literal JavaScript object, you can use an immediately invoked function

var data = {
     "label" : value,
     "sets" : (function(){
         var arr = [];
         for (var i=0; i < item.length; i++) {
             arr.push( {somedata: "data" + i} ) ;
         }
         return arr;
      })()
 };

As dystroy has pointed out You can also use Array.map to return a transformed array, without needing an immediately invoked function, which looks a little nicer

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

Comments

1

You may use functional programming :

var data = {
  "label" : "value",
  "sets" : item.map(function(_,i){ return {somedata: "data"+(i+1)} })  
}

7 Comments

This is not JSON, but if OP's goal is actually to create a JavaScript object, then this will work.
There's no JSON in OP's question
The question's title is "Is there possible to loop inside JSON object".
@Khalid JSON is a data exchange format, a serialization of plain objects in any language. We can create that JSON in C#, Java, or any other language
@JuanMendes is there a loop in json ?
|
1

Use the following:

var data = {
    label: value,
    get sets(){
        var array = [];
        /* write your logic to fill the array here. */
        return array;
    }
}

Reference here

2 Comments

This builds the array each time the value is read, which make it improper for almost any use
And this is not JSON either.
1

As others have commented, JSON is data, not code. It looks like you're making javascript code though, since JSON also wouldn't include the var data part.

  • JSON => JavaScript Object Notation, a wide-spread way of representing data.
  • javascsript object => A structure within the javascript programming language that uses JavaScript Object Notation.

You can do something like this.

var data = {
    "label" : 'my_label',
};

item = ['one','two','another'];
data.sets = [];
for (var i=0; i < item.length; i++)
{
    data.sets.push({'somedata': item[i]});
}

2 Comments

Good point, the OP has a var in there, so it's not JSON but I think the OP wants something inline? Also, "javascsript object => A structure within the javascript programming language that may use JavaScript Object Notation." JavaScript objects do not fully conform to JSON, no need to quote properties, and you can use single quotes.
@JuanMendes good point ... I was tempted to hedge with "looks like JSON" or "may be JSON" but that's almost flamebaiting on stackoverflow ... :-)
0

You can use array comprehension (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions), but it's not supported yet by all browsers (ECMAScript 6).

var value = "test";
var item = ["data1", "data2", "data3"];

var data = {
    "label" : value,
    "sets" : [for (x of item) {somedata: x}]
};

/*
    Result : 

    data = {
        "label":"test",
        "sets":[
            {"somedata":"data1"},
            {"somedata":"data2"},
            {"somedata":"data3"}
        ]
    }
*/

Comments

-4

You can have nested data in JSON like for example

var myObject = {
  "first": "John",
  "last": "Doe",
  "age": 39,
  "sex": "M",
  "salary": 70000,
  "registered": true,
  "interests": [ "Reading", "Mountain Biking", "Hacking" ],
  "favorites": {
    "color": "Blue",
    "sport": "Soccer",
    "food": "Spaghetti"
  }, 
  "skills": [
    {
      "category": "JavaScript",
      "tests": [
        { "name": "One", "score": 90 },
        { "name": "Two", "score": 96 }
      ] 
    },
    {
      "category": "CouchDB",
      "tests": [
        { "name": "One", "score": 79 },
        { "name": "Two", "score": 84 }
      ] 
    },
    {
      "category": "Node.js",
      "tests": [
        { "name": "One", "score": 97 },
        { "name": "Two", "score": 93 }
      ] 
    }
  ]
};

You can access such an array and its contents using a loop in your program

Source: http://www.json.com/

3 Comments

This doesn't seem to have anything to do with OP's question.
I answered wrt to accessing the data using a loop... i guess its not what the owner is looking for
If you look at the examples, it's clear that OP is trying to produce the data with a loop, not access it.

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.