6

我正在尝试编写一个方法来帮助我返回所有货币的对象键数组。但是,我被困在一个点上,我得到了带有键值对的完整对象数组。

是的,我主要需要使用 ES6 方法。我不想使用任何其他迭代器。

例如:我需要什么:

['AED', 'ALL', 'AUD', 'EUR' .....]

我得到什么:

[{AED: {"isDefault": true}}, {ALL: {"isDefault": true}}, {AUD: {"isDefault": true}}, {'EUR': {"isDefault": true}}.....]

你能帮我实现这个吗?

这是代码:

var myJSON = {
	  "countryCode": {
	  "Australia": "AU",
	  "United States": "US",
	  "Britain": "GB",
	  "Japan": "JP",
	  "India": "IND",
	  "France": "FR",
	  "Russia": "RS"
	},
	"countries": {
		"AE": {
		  "currencies": {
			"AED": {
			  "isDefault": true
			}
		  }
		},
		"AL": {
		  "currencies": {
			"ALL": {
			  "isDefault": true
			}
		  }
		},
		"AU": {
		  "currencies": {
			"AUD": {
			  "isDefault": true
			}
		  }
		},
		"US": {
		  "currencies": {
			"USD": {
			  "isDefault": true
			}
		  }
		},
		"GB": {
		  "currencies": {
			"EUR": {
			  "isDefault": true
			}
		  }
		},
		"FR": {
		  "currencies": {
			"EUR": {
			  "isDefault": true
			}
		  }
		},
		"JP": {
		  "currencies": {
			"JPY": {
			  "isDefault": true
			}
		  }
		},
		"RS": {
		  "currencies": {
			"RSD": {
			  "isDefault": false
			}
		  }
		},
		"ZA": {
		  "currencies": {
			"ZAR": {
			  "isDefault": true
			}
		  }
		}
	  }
	};
	
	function getData() {
	  const myArr = Object.keys(myJSON.countries).map((k) => myJSON.countries[k]);
	  console.log(myArr);	
	  const myArr1 = myArr.map((currency) => currency.currencies);
	  console.log(myArr1);
	  const myArr2 = myArr1.map((key, value) => key);
	  console.log(myArr2);
	}
<button onclick="getData()">Get Data</button>

4

4 回答 4

7

您可以获取对象的第一个键。

myArr1.map((key, value) => Object.keys(key)[0]);

function getData() {
    const result = Object
            .keys(myJSON.countries)
            .map(k => myJSON.countries[k])
            .map(({ currencies }) => currencies)
            .map(currency => Object.keys(currency)[0]);

    console.log(result);
}

var myJSON = { countryCode: { Australia: "AU", "United States": "US", Britain: "GB", Japan: "JP", India: "IND", France: "FR", Russia: "RS" }, countries: { AE: { currencies: { AED: { isDefault: true } } }, AL: { currencies: { ALL: { isDefault: true } } }, AU: { currencies: { AUD: { isDefault: true } } }, US: { currencies: { USD: { isDefault: true } } }, GB: { currencies: { EUR: { isDefault: true } } }, FR: { currencies: { EUR: { isDefault: true } } }, JP: { currencies: { JPY: { isDefault: true } } }, RS: { currencies: { RSD: { isDefault: false } } }, ZA: { currencies: { ZAR: { isDefault: true } } } } };
	
<button onclick="getData()">Get Data</button>

或者只需一步:

function getData() {
    const result = Object
            .keys(myJSON.countries)
            .map(k => Object.keys(myJSON.countries[k].currencies)[0]);

    console.log(result);
}

var myJSON = { countryCode: { Australia: "AU", "United States": "US", Britain: "GB", Japan: "JP", India: "IND", France: "FR", Russia: "RS" }, countries: { AE: { currencies: { AED: { isDefault: true } } }, AL: { currencies: { ALL: { isDefault: true } } }, AU: { currencies: { AUD: { isDefault: true } } }, US: { currencies: { USD: { isDefault: true } } }, GB: { currencies: { EUR: { isDefault: true } } }, FR: { currencies: { EUR: { isDefault: true } } }, JP: { currencies: { JPY: { isDefault: true } } }, RS: { currencies: { RSD: { isDefault: false } } }, ZA: { currencies: { ZAR: { isDefault: true } } } } };
	
<button onclick="getData()">Get Data</button>

于 2018-02-20T15:18:45.657 回答
2

您可以使用map&for..in循环来迭代对象

var myJSON = {
  "countryCode": {
    "Australia": "AU",
    "United States": "US",
    "Britain": "GB",
    "Japan": "JP",
    "India": "IND",
    "France": "FR",
    "Russia": "RS"
  },
  "countries": {
    "AE": {
      "currencies": {
        "AED": {
          "isDefault": true
        }
      }
    },
    "AL": {
      "currencies": {
        "ALL": {
          "isDefault": true
        }
      }
    },
    "AU": {
      "currencies": {
        "AUD": {
          "isDefault": true
        }
      }
    },
    "US": {
      "currencies": {
        "USD": {
          "isDefault": true
        }
      }
    },
    "GB": {
      "currencies": {
        "EUR": {
          "isDefault": true
        }
      }
    },
    "FR": {
      "currencies": {
        "EUR": {
          "isDefault": true
        }
      }
    },
    "JP": {
      "currencies": {
        "JPY": {
          "isDefault": true
        }
      }
    },
    "RS": {
      "currencies": {
        "RSD": {
          "isDefault": false
        }
      }
    },
    "ZA": {
      "currencies": {
        "ZAR": {
          "isDefault": true
        }
      }
    }
  }
};

function getData() {
  // get countries object
  let getCountries = myJSON.countries;
  // get all country short names in an array
  var ctr = Object.keys(getCountries);
  // iterate that array using map 
  var getCur = ctr.map(function(item) {
    // in countries object get the object where the country shortname
    // matches the object key. Get the curriencies usin for ..in loop
    for (let keys in getCountries[item].currencies) {
      return keys
    }
  })
  console.log(getCur)
}
<button onclick="getData()">Get Data</button>

于 2018-02-20T15:24:21.597 回答
2

单线:

result = [].concat(...Object.values(data.countries).map(x => x.currencies).map(Object.keys))

data你的对象在哪里

于 2018-02-20T15:31:38.633 回答
0

你可以简单地使用:
Object.keys(myJSON.countries).map(con => Object.keys(myJSON.countries[con].currencies));

于 2018-02-20T16:44:39.467 回答