0

値を取得したいオブジェクトの配列があります:

    lastChanceSeats: Object
      assignedSeats: Array[6]
        0: Object
          SeatId: "29A"
          passengerName: "Mr test1 test1"
          sectorSequence: 1
        1: Object
          SeatId: "22A"
          passengerName: "Mr test1 test1"
          sectorSequence: 2
        2: Object
          SeatId: "29B"
          passengerName: "Mrs test2 test2"
          sectorSequence: 1
        3: Object
          SeatId: "22B"
          passengerName: "Mrs test2 test2"
          sectorSequence: 2
        4: Object
          SeatId: "29C"
          passengerName: "Master test3 test3"
          sectorSequence: 1
        5: Object
          SeatId: "22C"
          passengerName: "Master test3 test3"
          sectorSequence: 2

次の形式でデータを提示したいと思います。

  • 最初の {passengerName}
  • {sectorSequence: 1} + {SeatId}
  • {sectorSequence: 2} + {SeatId}

...次に、乗客 2、3 などについても同様です。

4

3 に答える 3

1

シンプルなテンプレートを使用し、各項目をループして出力を生成します。

var data=[
    {
        "SeatId": "29A",
        "passengerName": "Mr test1 test1",
        "sectorSequence": 1
    },
    {
        "SeatId": "22A",
        "passengerName": "Mr test1 test1",
        "sectorSequence": 2
    },
    {
        "SeatId": "29B",
        "passengerName": "Mrs test2 test2",
        "sectorSequence": 1
    }
]


function template(ob, str){
   return str.replace(/{([\w\s.\$]+?)}/g, 
     function(j,a){  return ob[a]||"";  } );
}

strTemplate="First {passengerName}  {sectorSequence} + {SeatId}";

out=$(data).map(function(i,item){ return template(item, strTemplate)}).toArray().join("\n");

alert(out);

/* result:
First Mr test1 test1  1 + 29A
First Mr test1 test1  2 + 22A
First Mrs test2 test2  1 + 29B  */
于 2013-07-04T10:18:20.807 に答える
0

上記の回答と同じデータを使用して、このスニペットを試すことができます

var data=[
    {
        "SeatId": "29A",
        "passengerName": "Mr test1 test1",
        "sectorSequence": 1
    },
    {
        "SeatId": "22A",
        "passengerName": "Mr test1 test1",
        "sectorSequence": 2
    },
    {
        "SeatId": "29B",
        "passengerName": "Mrs test2 test2",
        "sectorSequence": 1
    }
]

function test_iteration(data) {
  data.forEach(e => {
    console.log(`SeatId : ${e.SeatId}`);
    console.log(`passengerName : ${e.passengerName}`);
    console.log(`sectorSequence : ${e.sectorSequence}`);
  })
}
function test_iteration_2(data) {
  data.forEach(e => {
    for (var prop in e) {
      if (Object.prototype.hasOwnProperty.call(e, prop)) {
        console.log(`${prop} : ${e[prop]}`);
      }
    }
  })
}

test_iteration_2(data) ;

于 2019-10-05T18:26:37.960 に答える