แผนที่กุญแจค่าที่จะสร้าง language โครงสร้างกับรอบการวัตถุการใช้จาวาสคริปต์

0

คำถาม

ฉันต้องการจะสร้างวัตถุของตารางคู่ลำดับจากจอแบนอาเรย์ซึ่งฉันจะเริ่มจากการขอข้อมูลและต้องการสร้าง language โครงสร้างเป็นการตอบสนอต้องผ่านมันเป็นรูปแบบ api การตอบสนองเลย สำหรับ eg- แบอาเรย์-

[{
    user_id: '2311123',
    user_country: 'United States',
    user_city: 'ny',
    ssn: 229
  },
  {
    user_id: '451313',
    user_country: 'United States',
    user_city: 'abc',
    ssn: 147
  },
  {
    user_id: '65345',
    user_country: 'United States',
    user_city: 'abc',
    ssn: 444
  },
  {
     user_id: '763343',
    user_country: 'Australia',
    user_city: 'auus',
    ssn: 678
  }]

ฉันต้องการสร้างโครงสร้างเหมือนคาดหวังผลส่งออก

{
  "United States": [
    {
      "ny": [
        {
          "user_id": "2311123",
          "ssn": "7"
        }
      ]
    },
    {
      "abc": [
        {
          "user_id": "451313",
          "ssn": "147"
        },
        {
          "user_id": "65345",
          "ssn": "444"
        }
      ]
    }
  ],
  "Australia": [
    {
      "auus": [
        {
          "user_id": "763343",
          "ssn": "678"
        }
      ]
    }
  ]
}

ซึ่งมี user_country อาเรย์ของวัตถุและ user_city อาเรย์ของวัตถุงแผนไว้. ฉันพยายามอดรหัสนี้,แต่ coudnt ก็จะประสบความสำเร็จในส่วนที่คาดหวังส่วนส่งออก:

  const map = {};
  results.forEach(arr => {
   console.log("arr",arr)
        if(map[arr.user_country]){
          if(!map[arr.user_country].includes(arr.user_city))
            map[arr.user_country].push(arr.user_city);
        }else{
          map[arr.user_country] = [arr.user_city]
        }
  });
  console.log(map);
arrays javascript json mapping
2021-11-24 06:12:50
4

คำตอบที่ดีที่สุด

2

นี่อาจจะผันคาดหวังผล:

const array = [{ user_id: '2311123', user_country: 'United States', user_city: 'ny', ssn: 229 }, { user_id: '451313', user_country: 'United States', user_city: 'abc', ssn: 147 }, { user_id: '65345', user_country: 'United States', user_city: 'abc', ssn: 444 }, { user_id: '763343', user_country: 'Australia', user_city: 'auus', ssn: 678 }];


const map = array.reduce((map, {user_country, user_city, ...userInfo}) => {
  if (!map[user_country]) {
    map[user_country] = [{[user_city]: [{...userInfo}]}];
  } else {
    const ex = map[user_country].find(city => Object.keys(city)[0] === user_city);
    if (!ex) {
      map[user_country].push({[user_city]: [{...userInfo}]});
    } else {
      Object.values(ex)[0].push({...userInfo});
    }
  }
  return map;
}, {});

console.log(map);

2021-11-24 06:42:25
1

โปรดตรวจสอบนี้ทางออก:

const map = {};
results.forEach(arr => {
    const { user_country, user_id, user_city, ssn } = arr;
    if (!map[user_country]) {
        map[user_country] = [];
    }

    if (map[user_country][user_city]) {
        map[user_country][user_city].push({user_id, ssn});
    } else {
        map[user_country][user_city] = [{user_id, ssn}];
    }
});

console.log(map)
2021-11-24 08:23:29
0

const results = [{
    user_id: '2311123',
    user_country: 'United States',
    user_city: 'ny',
    ssn: 229
  },
  {
    user_id: '451313',
    user_country: 'United States',
    user_city: 'abc',
    ssn: 147
  },
  {
    user_id: '65345',
    user_country: 'United States',
    user_city: 'abc',
    ssn: 444
  },
  {
    user_id: '763343',
    user_country: 'Australia',
    user_city: 'auus',
    ssn: 678
  }
]

const out = {};
results.forEach(i => {
  out[i.user_country] = out[i.user_country] || {};
  out[i.user_country][i.user_city] = out[i.user_country][i.user_city] || [];
  out[i.user_country][i.user_city].push({
    user_id: i.user_id,
    ssn: i.ssn
  })
})

console.log(out)

2021-11-24 06:39:44

ไม่ใช่ผลลัพธ์ที่ปฏิบัติการลังมองหานั่นคือทำไมฉันถึงถูกเพิ่มที่ออกความคิดเห็น แต่มันก็สมเหตุสมผลเป็นผลส่งออก fwiw. PS อย่าลืม coerce ที่ ssn เพื่อเป็นข้อความ
Andy
0

โปรดตรวจสอบว่าตัวเลือก:

const results = [{
    user_id: '2311123',
    user_country: 'United States',
    user_city: 'ny',
    ssn: 229
  },
  {
    user_id: '451313',
    user_country: 'United States',
    user_city: 'abc',
    ssn: 147
  },
  {
    user_id: '65345',
    user_country: 'United States',
    user_city: 'abc',
    ssn: 444
  },
  {
     user_id: '763343',
    user_country: 'Australia',
    user_city: 'auus',
    ssn: 678
  }];

const countries = {};

results.forEach(result => {
  const {user_country, user_city, user_id, ssn} = result;
  const cityList = countries[user_country] && countries[user_country][user_city] ? countries[user_country][user_city] : [];
  const newCityList = [...cityList, {
      user_id,
      ssn
    }];
  countries[user_country] = {
    ...countries[user_country],
    [user_city]: newCityList
  };
});

console.log(countries);

2021-11-24 06:43:28

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่