根据不同的密钥将值分组为一个对象数组

发布于 2025-02-10 07:47:38 字数 577 浏览 4 评论 0原文

我需要将两个对象分组到一个对象中。

开始对象:

{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青芜 2025-02-17 07:47:38

您可以在成分对象的键上循环,并通过切片成分编号并寻找该数字的相应度量来创建所需的数组。

const 
  ingredients = { strIngredient1: "Light rum", strIngredient2: "Lime", strIngredient3: "Sugar", strIngredient4: "Mint", strIngredient5: "Soda water" },
  measurements = { strMeasure1: "2-3 oz", strMeasure2: "Juice of 1", strMeasure3: "2 tsp", strMeasure4: "2-4" },
  result = Object.keys(ingredients).map((k) => ({
    ingredient: ingredients[k],
    measure: measurements[`strMeasure${k.slice(13)}`] ?? "",
  }));

console.log(result);

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.

const 
  ingredients = { strIngredient1: "Light rum", strIngredient2: "Lime", strIngredient3: "Sugar", strIngredient4: "Mint", strIngredient5: "Soda water" },
  measurements = { strMeasure1: "2-3 oz", strMeasure2: "Juice of 1", strMeasure3: "2 tsp", strMeasure4: "2-4" },
  result = Object.keys(ingredients).map((k) => ({
    ingredient: ingredients[k],
    measure: measurements[`strMeasure${k.slice(13)}`] ?? "",
  }));

console.log(result);

箜明 2025-02-17 07:47:38

您可以分开键并构建一个新对象。

const
    data = [{ 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 ' }],
    keys = { strIngredient: 'ingredient', strMeasure: 'measure' },
    result = data.reduce((r, o, i) => {
        Object.entries(o).forEach(([key, value]) => {
            const [, k, i] = key.match(/^(.*?)(\d+)$/);
            (r[i - 1] ??= { ingredient: '', measure: '' })[keys[k]] = value;
        });
        return r;        
    }, []);

console.log(result);

You could separate the keys and build a new object.

const
    data = [{ 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 ' }],
    keys = { strIngredient: 'ingredient', strMeasure: 'measure' },
    result = data.reduce((r, o, i) => {
        Object.entries(o).forEach(([key, value]) => {
            const [, k, i] = key.match(/^(.*?)(\d+)$/);
            (r[i - 1] ??= { ingredient: '', measure: '' })[keys[k]] = value;
        });
        return r;        
    }, []);

console.log(result);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文