如何提取数组中变量的总实例及其第一个属性
How to extract total instance of a variable in a array and its first properties
我已经得到了如下所示的水果数组
var fruits = [
{
"buyprice": "10",
"sellprice": "11",
"name": "pomogranate"
},
{
"buyprice": "10",
"sellprice": "11",
"name": "apple"
},
{
"buyprice": "12",
"sellprice": "13",
"name": "orange"
},
{
"buyprice": "14",
"sellprice": "15",
"name": "apple"
}
]
我想找出数组中存在的苹果的总数数组中苹果的第一个元素(不是最后一个元素)的买价和卖价
我已经试过了
function findnumberofaccourences(fruits, apple) {
var a = 0;
var buyprice;
var sellprice
for (var i = 0; i < fruits.length; i++) {
var name = fruits[i].name;
buyprice = fruits[i].buyprice;
sellprice = fruits[i].sellprice;
if (name == apple) {
a++;
}
}
var p = { count: a, buyprice: buyprice, sellprice: sellprice };
return p;
}
var result = findnumberofaccourences(fruits, 'apple');
alert(JSON.stringify(result));
但是当我运行这个时,我得到的结果是(苹果的最后元素的买价和卖价,因为我需要苹果的第一个元素)
{"count":2,"buyprice":"14","sellprice":"15"}
这是我的小提琴
http://jsfiddle.net/thkc0fpk/2/你能告诉他们如何做到这一点吗
几点:
- 如hackto所说,只抓
a == 0
时的价格。 - 还有,只抓苹果的价格,而不是所有水果的价格。
- 调用给出水果名称的参数来查找
apple
有点误导(因为它可能包含字符串pear
)。也许是fruitName
或类似的?
下面是一个最小变化的例子:
function findnumberofaccourences(fruits, fruitName) {
var a = 0;
var buyprice;
var sellprice;
for (var i = 0; i < fruits.length; i++) {
var name = fruits[i].name;
if (name == fruitName) { // Only apples
if (a == 0) { // Only the first
buyprice = fruits[i].buyprice;
sellprice = fruits[i].sellprice;
}
a++;
}
}
var p = {
count: a,
buyprice: buyprice,
sellprice: sellprice
};
return p;
}
var result = findnumberofaccourences(fruits, 'apple');
alert(JSON.stringify(result));
您还可以简化一点,使用更现代的forEach
并使用您将直接返回的对象:
function findnumberofaccourences(fruits, fruitName) {
var p = {
count: 0,
buyprice: undefined,
sellprice: undefined
};
fruits.forEach(function(fruit) {
if (fruit.name == fruitName) {
if (p.count == 0) {
p.buyprice = fruit.buyprice;
p.sellprice = fruit.sellprice;
}
p.count++;
}
});
return p;
}
var result = findnumberofaccourences(fruits, 'apple');
alert(JSON.stringify(result));
您可以先将苹果过滤到一个数组中,然后检索该数组的第一个元素以获得所需的数据。
var apples = fruits.filter(function (fruit) {
return fruit.name === 'apple';
});
var p = {
count: apples.length,
buyprice: apples[0].buyprice,
sellprice: apples[0].sellprice
};
先反转数组
var price = {
count: 0
};
fruits.reverse().forEach(function (fruit) {
if (fruit.name === "apple") {
price.count++;
price.buyprice = fruit.buyprice;
price.sellprice = fruit.sellprice;
}
})
console.log(price)
// restore original order in fruits array (if you are going to use it)
fruits.reverse();
注意,这将使你的数组反转,所以你需要再次执行fruits.reverse()
,如果你打算在流的后面使用水果,以原始顺序获得水果数组元素。(感谢aduch)
你的代码没有像你期望的那样工作,你的例子工作只是因为apple
是数组的最后一个元素。
你需要像这样更新你的for循环
for (var i = 0; i < fruits.length; i++) {
var name = fruits[i].name;
if (name == apple) {
if ( a === 0){
buyprice = fruits[i].buyprice;
sellprice = fruits[i].sellprice;
}
a++;
}
}
对于第一个apple
,它只会第一次更新buyprice
和sellprice
你原来的代码是更新buyprice
和sellprice
为每个元素的数组
相关文章:
- Backbone.Model.extend不创建实例属性
- 在定义了构造函数之后,是否可以将实例属性添加到JavaScript原型中
- 从原型方法更改实例属性
- 在 IIFE 中使用实例属性
- 在 ES6 中,允许解构类实例属性
- 在 ES6 中自动将参数设置为实例属性
- 将实例属性放置在原型构造函数中,无论好坏
- JS:原型-访问正确的实例属性
- 我可以在Silverlight中的Scriptable Member属性上设置Javascript中的实例属性吗
- 实例属性会模糊原型链吗
- mixin中的prototype.js实例属性
- 为什么不应该't我直接访问实例属性进行写操作
- Javascript”;私人的“;与实例属性相比
- 为什么在原型中声明实例属性而不是在构造函数中声明
- 如何用原型函数更新实例属性,并从另一个函数访问它
- 新的骨干实例属性指向旧的实例属性
- 在实例方法中获取对实例属性的访问权
- ES6类/实例属性
- 为什么赋值影响实例属性而不是原型属性?
- 在Coffee Script中理解类扩展和实例属性