简体   繁体   English

使用另一个对象的键和值创建一个新对象

[英]Create a new object using keys and values from another object

I'm saving an object in state that looks like: 我正在将对象保存为如下所示的状态:

ingredients: {
        salad: {
            amount: 3,
            basePrice: 1
        },
        cheese: {
            amount: 2,
            basePrice: 1.2
        }
}

I want to use this in my component's state as follows 我想在组件的状态下使用它,如下所示

    ingredients: {
    salad: 3,
    cheese: 2
}

I used Object.keys and map at first but it returns an Array of key value pairs instead of objects. 我最初使用Object.keysmap ,但是它返回键值对数组而不是对象。

Although this works: 尽管这可行:

  const newIngredientsObject = {}; for (const i in ingredientsObject) newIngredientsObject[i] = ingredientsObject[i].amount; return newIngredientsObject; 

I want to find a solution without a helper method, as such: 我想找到一个没有助手方法的解决方案,例如:

 const mapStateToProps = state => { return { ingredients: Object.keys(state.burger.ingredients).map(i => ( { [i]: state.burger.ingredients[i].amount } )), totalPrice: state.burger.totalPrice } }; 

You could map the entries of the object by using the amount property of the values and take Object.assign for getting a new object. 您可以使用值的amount属性映射对象的条目,并使用Object.assign来获取新对象。

 var ingredients = { salad: { amount: 3, basePrice: 1 }, cheese: { amount: 2, basePrice: 1.2 } }, result = Object.assign( ...Object.entries(ingredients).map(([key, value]) => ({ [key]: value.amount })) ); console.log(result); 

when you want to create one object to inside to another go this web 当您想在另一个对象内部创建一个对象时,请访问此网站

object 1  = {
content 1 = {
    stuff = {
    },
    more Stuff = {
    }
}

} object 2 = { key 1:"", key 2:"" }对象2 = {键1:“”,键2:“”

} object 1.content 1 = object 2; } object 1.content 1 = object 2;

You asked a solution with Object.keys, here is one: 您用Object.keys提出了一个解决方案,这是一个:

var x = {ingredients: {
        salad: {
            amount: 3,
            basePrice: 1
        },
        cheese: {
            amount: 2,
            basePrice: 1.2
        }
}}

Object.keys(x.ingredients).map((d,i)=>[d,x.ingredients[d].amount]).reduce((ac,d,i)=>(ac[d[0]]=d[1],ac),{})

//{salad: 3, cheese: 2}

The shortest I could come up with it using Object.keys and reduce 我可以使用Object.keys reduce它的最短时间

 const ingredients = { salad: { amount: 3, basePrice: 1 }, cheese: { amount: 2, basePrice: 1.2 } }; let newObject = Object.keys(ingredients).reduce((acc, val) => { acc[val] = ingredients[val].amount; return acc; }, {}); console.log(newObject); 

ANSWER : Thanks for the solutions provided by @ibowankenobi and @Nina Scholz 解答 :感谢@ibowankenobi@Nina Scholz提供的解决方案

  • Reduce function: 缩小功能:

 Object.keys(ingredients) .map( (d, i) => [d, ingredients[d].amount]) .reduce( (ac, d, i) => (ac[d[0]] = d[1], ac), {} ); 

  • Entries function: 条目功能:

 Object.assign(...Object.entries(ingredients) .map( ([key, value]) => ({ [key]: value.amount }) ) ); 

reduce: 0.0849609375ms
entries: 0.1650390625ms
forLoop: 0.07421875ms
reduce: 0.024169921875ms
entries: 0.048095703125ms
forLoop: 0.010009765625ms
reduce: 0.010009765625ms
entries: 0.016845703125ms
forLoop: 0.0078125ms

After some testing, it seems like using a for loop is the most efficient and after that the reduce function. 经过一些测试,似乎使用for循环是最有效的,然后是reduce函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM