【问题标题】:ES6 Map to return an array of object keys onlyES6 Map 仅返回对象键数组
【发布时间】:2018-02-20 15:15:05
【问题描述】:

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

是的,我主要需要使用 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>

【问题讨论】:

    标签: javascript arrays object ecmascript-6 es6-map


    【解决方案1】:

    你可以取对象的第一个键。

    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>

    【讨论】:

      【解决方案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>

      【讨论】:

      • 谢谢 :) 但是,不幸的是,我不想使用任何原始的 javascript 迭代器。
      • 这有什么具体原因吗?如果您担心性能手册 for 循环实际上要快得多,即使可读性稍差。
      【解决方案3】:

      单线:

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

      data 是你的对象

      【讨论】:

      • 哇...太棒了!
      • @Sunny:显然不够
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2018-01-02
        • 2018-07-31
        • 1970-01-01
        • 2017-03-13
        • 1970-01-01
        • 2023-02-15
        • 2015-12-16
        • 2014-07-14
        相关资源
        最近更新 更多