简体   繁体   English

是否有可能在JavaScript对象文字中循环

[英]Is there possible to loop inside JavaScript object literal

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. 很多帮助。

As jimm101 has pointed out , you are not working with JSON, that's just JavaScript (the var in there proves it) . 正如jimm101所指出的 ,您不使用JSON,而只是使用JavaScript(其中的var证明了这一点)。 If you want to calculate a value inside a literal JavaScript object, you can use an immediately invoked function 如果要在文字JavaScript对象中计算值,则可以使用立即调用的函数

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 dystroy指出,您还可以使用Array.map返回转换后的数组,而无需立即调用的函数,看上去更好

You may use functional programming : 您可以使用函数式编程:

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

Use the following: 使用以下内容:

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

Reference here 这里参考

As others have commented, JSON is data, not code. 正如其他人所评论的那样,JSON是数据,而不是代码。 It looks like you're making javascript code though, since JSON also wouldn't include the var data part. 不过,由于JSON也不会包含var data部分,因此您似乎正在制作javascript代码。

  • JSON => JavaScript Object Notation, a wide-spread way of representing data. JSON => JavaScript Object Notation,一种表示数据的广泛方式。
  • javascsript object => A structure within the javascript programming language that uses JavaScript Object Notation. javascsript object => javascript编程语言中使用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]});
}

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). 您可以使用数组理解( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions ),但并非所有浏览器都支持它(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"}
        ]
    }
*/

You can have nested data in JSON like for example 您可以在JSON中嵌套数据,例如

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/ 资料来源: http : //www.json.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM