Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
371 มุมมอง

Hi every body!!!

I want to create a json object from an array with javascript. But, what i get through the function below is not what i want.

Can anyone help me.

Here is the code and the result:

var x = tabResult[0];
            for(var k= 0; k < x.length; k++)
        {
                console.log(x[k+1]);
             var my_json =
               {"day":x[k+1]
           }

Object {day: "1"}

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hello, not sure what you want. 

But i think you looking for the Array.prototype.map()-function.

You can iterate over an array and create a new one like:

var array= [[1,2,3],[4,5,6]];
   var jsonObject = array.map(function(value,index,arr){
       var generateNewJson = {
           "first":value[0],
           "second":value[1],
           "third":value[2]
       };
       return generateNewJson;
   });
   
   //jsonObject contains now:
   //[{first:1,second:2,third:3},{first:4,second:5,third:6}]

More information about how to use the map-function you can get here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Hope that helps you.

อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

Thanks a lot for your answer ;)

But  want to ask you if you can help me:

Do you know how to use this.fild_manager.set_values???

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
0
ก.พ. 21
3105
1
พ.ค. 25
760
0
มี.ค. 25
405
1
ธ.ค. 24
2956
2
พ.ย. 24
1219