如何用另一个对象的键更改对象的键

时间:2019-01-18 15:03:46

标签: javascript arrays json object

我被困于将对象结果的键更改为另一个对象的另一个键,也许我的代码不是动态的,我很喜欢从这里得到一些提示:

function country(arr) {
  var obj = {},
    temp = {}
  for (i of arr) {
    if (i.length === 2) {
      if (!temp[i[0]])
        temp[i[0]] = []
      temp[i[0]].push(i[1])
    }

    if (i.length === 3) {
      obj[i[2]] = []
    }

  }
  console.log(obj) // final result
  console.log(temp)

}

var cities = [
  ["c", "br", "Brazil"],
  ["br", "Rio de Jeneiro"],
  ["c", "usa", "United States"],
  ["ru", "St. Petersburg"],
  ["usa", "New York"],
  ["ksa", "Mekkah"],
  ["usa", "Washington DC"],
  ["usa", "California"],
  ["c", "ch", "China"],
  ["ksa", "Madinah"],
  ["ch", "Beijing"],
  ["c", "ind", "India"],
  ["ch", "Shanghai"],
  ["ind", "Bangalore"],
  ["ind", "New Delhi"],
  ["c", "ru", "Rusia"],
  ["ru", "Moscow"],
  ["c", "ksa", "Arab Saudi"]
]

console.log(country(cities))

variable obj是我要输出的密钥,

所以我想要的输出是这样的:

{
      Brazil: [ 'Rio de Jeneiro' ],
      'United States': [ 'New York', 'Washington DC', 'California' ],
     ............ rest...
}

是否可以将temp对象的键obj更改为变量obj的键?

2 个答案:

答案 0 :(得分:2)

以下内容利用filtermapforEach等一些数组方法来按要求格式化数据。

var cities = [
  ["c", "br", "Brazil"],
  ["br", "Rio de Jeneiro"],
  ["c", "usa", "United States"],
  ["ru", "St. Petersburg"],
  ["usa", "New York"],
  ["ksa", "Mekkah"],
  ["usa", "Washington DC"],
  ["usa", "California"],
  ["c", "ch", "China"],
  ["ksa", "Madinah"],
  ["ch", "Beijing"],
  ["c", "ind", "India"],
  ["ch", "Shanghai"],
  ["ind", "Bangalore"],
  ["ind", "New Delhi"],
  ["c", "ru", "Rusia"],
  ["ru", "Moscow"],
  ["c", "ksa", "Arab Saudi"]
]

let lookup = {};

cities.filter(city => city[0] === "c").forEach(country => {
  let matches = cities
    .filter(city => city[0] === country[1])
    .map(city => city[1]);
  lookup[country[2]] = matches;
});

console.log(lookup);

编辑:这是不使用数组函数的解决方案。

var cities = [
  ["c", "br", "Brazil"],
  ["br", "Rio de Jeneiro"],
  ["c", "usa", "United States"],
  ["ru", "St. Petersburg"],
  ["usa", "New York"],
  ["ksa", "Mekkah"],
  ["usa", "Washington DC"],
  ["usa", "California"],
  ["c", "ch", "China"],
  ["ksa", "Madinah"],
  ["ch", "Beijing"],
  ["c", "ind", "India"],
  ["ch", "Shanghai"],
  ["ind", "Bangalore"],
  ["ind", "New Delhi"],
  ["c", "ru", "Rusia"],
  ["ru", "Moscow"],
  ["c", "ksa", "Arab Saudi"]
];

let lookup = {};

for (let i = 0; i < cities.length; i++) {
  // Test if this is a country
  if (cities[i][0] === "c") {
    // Make sure we have not processed this country before
    if (!lookup[cities[i][2]]) {
      lookup[cities[i][2]] = [];
      
      let countryCode = cities[i][1];
      // Loop through cities again and match country code
      for (let j = 0; j < cities.length; j++) {
        if (cities[j][0] === countryCode) {
          lookup[cities[i][2]].push(cities[j][1]);
        }
      }
    }
  }
}

console.log(lookup);

答案 1 :(得分:0)

这是一个带有循环的例子:

const cities = [["c","br","Brazil"],["br","Rio de Jeneiro"],["c","usa","United States"],["ru","St. Petersburg"],["usa","New York"],["ksa","Mekkah"],["usa","Washington DC"],["usa","California"],["c","ch","China"],["ksa","Madinah"],["ch","Beijing"],["c","ind","India"],["ch","Shanghai"],["ind","Bangalore"],["ind","New Delhi"],["c","ru","Rusia"],["ru","Moscow"],["c","ksa","Arab Saudi"]];

const out = {};
const lookup = {};

// First loop creates a lookup of the
// short and long country names,
// and initialises the output object
for (let city of cities) {
 if (city.length === 3) {
   const [ , short, full ] = city;
   out[full] = out[full] || [];
   lookup[short] = full;
 }
}

// Second loop finds the actual cities
// and, checks the lookup table, then adds them to
// the output object
for (let city of cities) {
 if (city.length === 2) {
   const [ short, full ] = city;
   const check = lookup[short];
   if (lookup[short]) out[check].push(full);
 }
}

console.log(out);