根据不同的密钥将值分组为一个对象数组
我需要将两个对象分组到一个对象中。
开始对象:
{strIngredient1: 'Light rum',
strIngredient2: 'Lime',
strIngredient3: 'Sugar',
strIngredient4: 'Mint',
strIngredient5: 'Soda water'}
{strMeasure1: '2-3 oz ',
strMeasure2: 'Juice of 1 ',
strMeasure3: '2 tsp ',
strMeasure4: '2-4 '}
我希望最终的对象看起来像这样:
[
{ingredient: 'Light rum', measure: '2-3 oz'},
{ingredient: 'Lime', measure: 'Juice of 1'},
etc... (if no measure, fill with empty string)
]
我尝试根据键和值将对象分解为两个数组,然后循环遍历数组并基于键中的数字输出值,但是那里必须是获得预期结果的一种更有效的方法。有什么建议吗?
I need to group two objects into an array of objects.
Beginning objects:
{strIngredient1: 'Light rum',
strIngredient2: 'Lime',
strIngredient3: 'Sugar',
strIngredient4: 'Mint',
strIngredient5: 'Soda water'}
{strMeasure1: '2-3 oz ',
strMeasure2: 'Juice of 1 ',
strMeasure3: '2 tsp ',
strMeasure4: '2-4 '}
I would like the final array of object to look like this:
[
{ingredient: 'Light rum', measure: '2-3 oz'},
{ingredient: 'Lime', measure: 'Juice of 1'},
etc... (if no measure, fill with empty string)
]
I have tried parsing the objects into two arrays based on keys and values, then looping through both arrays and outputting the values based on the number in the keys, however there has to be a more efficient way of getting the desired outcome. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在成分对象的键上循环,并通过切片成分编号并寻找该数字的相应度量来创建所需的数组。
You can loop over the keys of the ingredients object and create the desired array by slicing out the ingredient number and looking for the corresponding measure for that number.
您可以分开键并构建一个新对象。
You could separate the keys and build a new object.