将对象数组转换为对象对象,以便新对象由其属性之一引用

时间:2017-08-24 11:37:03

标签: javascript arrays javascript-objects

我基本上是在寻找快捷方式来在javascript中执行此操作:

let arr = [{key: "alpha", value: "2", property1: "some"},{key:"beta", value:"3", property1: "extra", property2: "property"},{key: "gamma", value: "4"}];
let obj = someFunction(arr);
// WANTED RESULT:
//obj is now { "alpha" : { value: "2", property1: "some" }, "beta" : {value: "3", property1: "extra", property2: "property",} , "gamma": {value: "4"} }

除了键之外,数组中的每个对象都可以有更多属性和不同属性。

编辑:这就是我所做的。它仍然在对象上留下属性,我不确定这种/循环是否是最有效的方法。

tempObj = {};
for (let i = 0; i < arr.length; i++) {
    tempObj[arr[i].key] = arr[i];
}
//this will give this answer, close enough to what I want: 
//{ alpha: {key: "alpha", value: "2", property1: "some"}, beta: key:"beta", value:"3", property1: "extra", property2: "property", gamma: {key: "gamma", value: "4"} }

5 个答案:

答案 0 :(得分:1)

这样做

    public static void main(String[]  args) throws MalformedURLException, InterruptedException{

    String URL = "http://www.DemoQA.com";
    String Node = "http://localhost:4444/wd/hub";
    DesiredCapabilities cap = DesiredCapabilities.firefox();
    cap.setBrowserName("firefox");
    cap.setPlatform(Platform.WIN10);


    driver = new RemoteWebDriver(new URL(Node), cap);

    driver.navigate().to(URL);
    Thread.sleep(5000);
    driver.quit();
}

答案 1 :(得分:0)

你可以这样做

let arr = [{key: "alpha", value: "2", property1: "some"},{key:"beta", value:"3", property1: "extra", property2: "property"},{key: "gamma", value: "4"}];

let result = {};

for(let element of arr){
    result[element.key] = element;
}

console.log(result);

答案 2 :(得分:0)

您可以使用Object.assign在迭代时使用新对象分配新属性。

&#13;
&#13;
var array = [{ key: "alpha", value: "2", property1: "some" }, { key: "beta", value: "3", property1: "extra", property2: "property" }, { key: "gamma", value: "4" }],
    object = array.reduce((r, o) =>
        Object.assign(r, {
            [o.key]: Object.keys(o)
                .reduce((s, k) => Object.assign(s, k !== 'key' ? { [k]: o[k] } : {}), {})
            }),
        {});

console.log(object);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果您还想删除可以使用的丰富key属性:

const array = [{key: "alpha", value: "2"}, {key: "beta", value: "3"}, {key: "gamma", value: "4", additional: "sample"}];
const obj = {};
array.forEach(item => {obj[item.key] = item; delete item['key'];});
console.log(obj);
console.log(array);

但请注意,原始数组的条目会被修改(关键属性会被删除)。如果不是这样,你必须(深?)克隆项目:

const array = [{key: "alpha", value: "2"}, {key: "beta", value: "3"}, {key: "gamma", value: "4", additional: "sample"}];

const obj = {};
array.forEach(item => {
  obj[item.key] = Object.assign({}, item);
  delete obj[item.key]['key'];
});
console.log(obj);
console.log(array);

但请注意浏览器对Object.assign的支持,如果您想支持IE,则需要polyfill(也可能用于forEach并替换箭头功能。)

答案 4 :(得分:0)

谢谢大家。它看起来会涉及某种循环和删除操作,由于性能问题,我将避免使用它:How do I remove a property from a JavaScript object?

tempObj = {};
for (let i = 0; i < arr.length; i++) {
    tempObj[arr[i].key] = arr[i];
}
//this will give this answer, close enough to what I want: 
//{ alpha: {key: "alpha", value: "2", property1: "some"}, beta: key:"beta", value:"3", property1: "extra", property2: "property", gamma: {key: "gamma", value: "4"} }