简体   繁体   English

如何在javascript中获取特定键的值

[英]how to get values of specific key in javascript

I have this Object我有这个对象

const fruits = {
    apple: 28,
    orange: 17,
    pear: 54,
};

I want to insert the values from the key "apple" into an empty array我想将键“apple”中的值插入到一个空数组中

with Object.values.fruits I get all the values, so I tried it with Object.values(fruits.apple) to specify the key使用Object.values.fruits我得到了所有的值,所以我尝试使用Object.values(fruits.apple) to specify the key

const leere_liste = [];

const values = Object.values(fruits);

leere_liste.push(values);

but this gives my an empty array, is there a method for that which I'm unaware of ?但这给了我一个空数组,有没有我不知道的方法?

Couple of things.几件事。

Its Object.values(fruits) , not Object.values.fruits .它的Object.values(fruits) ,而不是Object.values.fruits

Object.values only works on objects. Object.values仅适用于对象。 So while it will work on fruits , it won't work on a specific property of fruits unless that property contains an object too.因此,尽管它会在工作fruits ,它不会对特定属性的工作fruits ,除非该属性包含的对象了。

Works:作品:

const fruits = {apple: 28, orange: 17, pear: {a: 54, b: 22}};

console.log(Object.values(fruits.pear));
// [54, 22]

Also, since you're getting an array back from Object.values , if you push that onto another array you'll just have an array containing an array of those values.此外,由于您从Object.values获取一个数组,如果您将它推送到另一个数组,您将只拥有一个包含这些值数组的数组。

If you want a single dimensional array, simply assign it like:如果你想要一个一维数组,只需像这样分配它:

const leere_liste = Object.values(fruits);

Not sure what " I get all the values, so I tried it with Object.values(fruits.apple) to specify the key" means, as in your code you are simply getting the values of the entire array, not just apple, and also the property apple itself doesn't have "values", it has only one value.不确定“我得到了所有值,所以我尝试使用 Object.values(fruits.apple) 来指定键”意味着什么,因为在您的代码中,您只是获取整个数组的值,而不仅仅是苹果,并且苹果本身也没有“值”,它只有一个值。

Object.values is a function that takes an object as a parameter and returns an array of all of the values of the object only, usually in alphabetical order of the keys, but the actual final product has no connection to the original keys or the object, just given it itself it would be impossible to determine what values correspond to what keys Object.values 是一个函数,它以对象为参数,只返回对象所有值的数组,通常按键的字母顺序排列,但实际最终产品与原始键或对象没有任何联系,仅仅给定它本身就不可能确定哪些值对应于哪些键

Hence it's a bit difficult to understand what you are trying to do, if you are trying to only insert the value of "apples" into an empty array, I don't know why you can't just do emptyArray.push(fruits.apple) , if you are trying to make a method for pushing the value of any given fruit to an array, you can simply use bracket notation on a function const fruity= (arr, obj, key) => arr.push(obj[key]) then to use it with your variables fruity(leere_liste, fruits, "apple")因此,理解您要做什么有点困难,如果您只想将“apples”的值插入空数组,我不知道为什么您不能只执行emptyArray.push(fruits.apple) ,如果您想制作一种方法将任何给定水果的值推送到数组中,您可以简单地在函数上使用括号表示法const fruity= (arr, obj, key) => arr.push(obj[key])然后将它与变量一起使用fruity(leere_liste, fruits, "apple")

If you want to convert the keys AND values into a new kind of array that you can work with, then what you need is Object.entries , it is a function that takes an object as a parameter and returns a two dimensional array, with the first element of each sub array representing the key of the respective object property, and the second element of each sub array representing the corresponding value to said key of the original object.如果要将键和值转换为可以使用的新类型的数组,那么您需要的是Object.entries ,它是一个将对象作为参数并返回二维数组的函数,其中每个子数组的第一个元素表示相应对象属性的键,每个子数组的第二个元素表示原始对象的所述键的对应值。

To use it just call var newArray=Object.entries(fruits) , in general this is helpful if you want to modify objects using expressions, such as Array.map, Array.forEach etc, although I'm not sure if you need it to simply add a property of an object to an array, to do that, see paragraphs above要使用它,只需调用var newArray=Object.entries(fruits) ,通常,如果您想使用表达式修改对象(例如 Array.map、Array.forEach 等var newArray=Object.entries(fruits) ,这会很有帮助,尽管我不确定您是否需要它简单地将对象的属性添加到数组中,为此,请参阅上面的段落

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

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