简体   繁体   English

如何在 java 脚本中的对象数组中多个值

[英]How to multiple a value in Array of objects in java script

This is my array.这是我的阵列。

let menu = [
            { id: 1,  name: "Soda",price: 3.12,size: "4oz",type: "Drink" },
            { id: 2, name: "Beer", price: 6.50, size: "8oz", type: "Drink" },
            { id: 3, name: "Margarita", price: 12.99, size: "12oz", type: "Drink" },
            { id: 4, name: "Pizza", price: 25.10, size: "60oz", type: "Food" },
            { id: 5, name: "Kebab", price: 31.48, size: "42oz", type: "Food" },
            { id: 6, name: "Berger", price: 23.83, size: "99oz", type: "Food" }
        ]


I want to multiple prices in 1.8 and also double the sizes我想在 1.8 的多个价格中增加一倍的尺寸
Then I want to show the main menu after these changes然后我想在这些更改后显示主菜单
This is my code and What I did.这是我的代码和我所做的。 This shows only prices and sizes, but I want the whole menu.这仅显示价格和大小,但我想要整个菜单。

Code:代码:

 let menu = [ { id: 1, name: "Soda",price: 3.12,size: "4oz",type: "Drink" }, { id: 2, name: "Beer", price: 6.50, size: "8oz", type: "Drink" }, { id: 3, name: "Margarita", price: 12.99, size: "12oz", type: "Drink" }, { id: 4, name: "Pizza", price: 25.10, size: "60oz", type: "Food" }, { id: 5, name: "Kebab", price: 31.48, size: "42oz", type: "Food" }, { id: 6, name: "Berger", price: 23.83, size: "99oz", type: "Food" } ] let prices = menu.map(item => item.price); multipliedPrice = prices.map(item => (item * 1.8).toFixed(2)); //------------------------------------------------------- doubledSize = menu.map(item => 2 * parseInt(item.size)); console.log("Size in USA is: " + doubledSize); console.log("Price in USA is: " + multipliedPrice);

Instead of returning the cacuated price only, return a new object like this:与其只返回计算价格,不如返回一个新的 object,如下所示:

menu.map(item => {
  return { ...item,
    price: 2 * parseInt(item.size)
  }
});
let prices = menu.map(item => { item.price = (item.price * 1.8).toFixed(2); return item;});
  • If you just change the price as your need and return all item, you can display all your array items.如果您只是根据需要更改价格并返回所有项目,则可以显示所有数组项目。

You can use Array.map for this purpose, we'll multiply each price by 1.8 and then double the size of each item:为此,您可以使用Array.map ,我们将每个价格乘以 1.8,然后将每个项目的大小加倍:

 let menu = [ { id: 1, name: "Soda",price: 3.12,size: "4oz",type: "Drink" }, { id: 2, name: "Beer", price: 6.50, size: "8oz", type: "Drink" }, { id: 3, name: "Margarita", price: 12.99, size: "12oz", type: "Drink" }, { id: 4, name: "Pizza", price: 25.10, size: "60oz", type: "Food" }, { id: 5, name: "Kebab", price: 31.48, size: "42oz", type: "Food" }, { id: 6, name: "Burger", price: 23.83, size: "99oz", type: "Food" } ]; const newMenu = menu.map(item => { return {...item, price: item.price * 1.8, size: parseInt(item.size) * 2 + "oz" } }) console.log("New menu:"); console.log("Item".padEnd(20) + "Size in USA".padEnd(20) + "Price in USA"); newMenu.forEach(item => console.log(item.name.padEnd(20) + (item.size + "").padEnd(20) + item.price.toFixed(2)))

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

相关问题 如何访问对象数组的对象数组下的键值对-java脚本 - How to access key value pairs under array of objects of array of objects - java script 如何检索对象名称的Java Script数组 - How to retrieve Java Script Array of Objects names 如何比较数组的多个对象中的属性值? - How to compare a property value in multiple objects of an array? 如何在 Java 脚本/TestCafe 中获取多个键值对数组 - How to fetch multiple key-value pairs array in Java Script/TestCafe 如果此值等于对象数组的特定属性,则 Java 脚本检查排除数组的每个值 - Java script check exclude each value of array if this value is equal to specific attribute of array of objects 如何将多个(未知数)对象(作为参数)传递给函数-Java脚本 - How to pass multiple (unknown number) objects (as a parameter) to a function - Java Script 我如何在java脚本中创建一个对象数组 - How can i make an array of objects in java script 如何在Java脚本中声明未知大小的用户定义对象数组 - How to declare an unknown sized array of user defined objects in java script 如何根据所选对象本身过滤数组 Java 脚本 - How to filter array based on selected objects itself Java script 如何将键值对添加到具有多个值的 javascript 对象数组中? - How to add a key value pair to an array of objects in javascript with multiple value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM