循环遍历和打印嵌套的JSON对象成员

时间:2018-08-30 18:07:22

标签: javascript json angular

我已经成功返回了我要搜索的对象(商店),但是现在我想遍历服务员数组并为商店打印服务员。我不能使用jQuery $ each()进行迭代。我能做什么?谢谢,谢谢您的协助。

totals.js(我的服务)

<?php

//getting the data from the Database

$aVar = mysqli_connect('localhost','root','','socialnetwork');
$usernameaccount = mysqli_query($aVar, "SELECT username FROM users WHERE username = '$username'")->fetch_array()[0];

$age = mysqli_query($aVar, "SELECT p.age FROM profiles p, users u WHERE u.username = '$username' AND u.id = p.id")->fetch_array()[0];

$gender = mysqli_query($aVar, "SELECT p.gender FROM profiles p, users u WHERE u.username = '$username' AND u.id = p.id")->fetch_array()[0];

$description = mysqli_query($aVar, "SELECT p.description FROM profiles p, users u WHERE u.username = '$username' AND u.id = p.id")->fetch_array()[0];

//after getting the data i insert them into a variable with some html code:

$outputusername = '<h2>'.$usernameaccount.'<hr></h2>';
$outputage = '<h2>'.$age.'<hr></h2>';
$outputgender = '<h2>'.$gender.'<hr></h2>';
$outputdescription = '<h2>'.$description.'<hr></h2>';

?>

tender-total-data.json

export const baseUrl = '/src/areas/store-totals/services/tender-total-data.json';
export const all = args =>

  // http.post.standard(baseUrl + 'orders/tenderTotals', {}, args);
  http.get(baseUrl).
  then(function onSuccess(response) { 
    console.log("The response is :" , response);        
  }).
  catch(function onError(response) {
   console.log(response);
  });

2 个答案:

答案 0 :(得分:0)

如果要获取特定商店的服务员,可以按以下方式使用array.find,

let data ={
  "stores": [
    {
      "storeName": "Master Bistro",
      "storeId": "3046",
      "attendants": [
        {
          "attendantName": "Janis Joplin",
          "attendantId": "9784526",
          "total": 2000,
          "tenderTotal": {
            "Cash": 500,
            "TC": 0,
            "UOD": 500,
            "MC": 250,
            "VI": 250,
            "AX": 250,
            "DI": 250,
            "JC": 0,
            "DC": 0,
            "UOP": 0,
            "GN": 0,
            "UOGC": 0,
            "HOTEL": 0,
            "NCTNCG": 0
          }
        },
        {
          "attendantName": "David Bowie",
          "attendantId": "2589456",
          "total": 14675,
          "tenderTotal": {
            "Cash": 175,
            "TC": 0,
            "UOD": 100,
            "MC": 9500,
            "VI": 3500,
            "AX": 550,
            "DI": 850,
            "JC": 0,
            "DC": 0,
            "UOP": 0,
            "GN": 0,
            "UOGC": 0,
            "HOTEL": 0,
            "NCTNCG": 0
          }
        },
        {
          "attendantName": "Michael Jackson",
          "attendantId": "5478264",
          "total": 15599,
          "tenderTotal": {
            "Cash": 250,
            "TC": 0,
            "UOD": 80,
            "MC": 5624,
            "VI": 6895,
            "AX": 2500,
            "DI": 250,
            "JC": 0,
            "DC": 0,
            "UOP": 0,
            "GN": 0,
            "UOGC": 0,
            "HOTEL": 0,
            "NCTNCG": 0
          }
        }
      ],
      "message": "Store totals for 08/20/2018",
      "date": "08/20/2018"
    },
    {
      "storeName": "The Master  Marketplace",
      "storeId": "3047",
      "attendants": [
        {
          "attendantName": "Dirk Novitski",
          "attendantId": "9784527",
          "total": 2000,
          "tenderTotal": {
            "Cash": 500,
            "TC": 0,
            "UOD": 500,
            "MC": 250,
            "VI": 250,
            "AX": 250,
            "DI": 250,
            "JC": 0,
            "DC": 0,
            "UOP": 0,
            "GN": 0,
            "UOGC": 0,
            "HOTEL": 0,
            "NCTNCG": 0
          }
        },
        {
          "attendantName": "Carmello Anthony",
          "attendantId": "2589458",
          "total": 14675,
          "tenderTotal": {
            "Cash": 175,
            "TC": 0,
            "UOD": 100,
            "MC": 9500,
            "VI": 3500,
            "AX": 550,
            "DI": 850,
            "JC": 0,
            "DC": 0,
            "UOP": 0,
            "GN": 0,
            "UOGC": 0,
            "HOTEL": 0,
            "NCTNCG": 0
          }
        },
        {
          "attendantName": "Stevie Wonder",
          "attendantId": "5478266",
          "total": 15599,
          "tenderTotal": {
            "Cash": 250,
            "TC": 0,
            "UOD": 80,
            "MC": 5624,
            "VI": 6895,
            "AX": 2500,
            "DI": 250,
            "JC": 0,
            "DC": 0,
            "UOP": 0,
            "GN": 0,
            "UOGC": 0,
            "HOTEL": 0,
            "NCTNCG": 0
          }
        }
      ],
      "message": "Store totals for 08/22/2018",
      "date": "08/21/2018"
    }
  ]
};

let attendants = data.stores.find(store=>store.storeId == 3047).attendants;
 
attendants.forEach( (element) => {
  console.log(element);
});

答案 1 :(得分:0)

要迭代数组元素,可以使用诸如map或forEach之类的函数。如果使用map,则迭代逻辑将类似于:

attendants.map((item) => {
 console.log(item);
});