从数组中的对象添加值

时间:2017-02-23 17:10:24

标签: javascript arrays javascript-objects

我有一个对象数组:

var array = [{
    id: "cards",
    amount: 5
}, {
    id: "shirts",
    amount: 3
}, {
    id: "cards",
    amount: 2
}, {
    id: "shirts",
    amount: 3
}]

我需要做的是遍历这个数组并找到所有id类型的总和。 所以在这个例子中,我会找到卡片和衬衫的总数。

我不确定如何使用对象执行此操作。我尝试用Object.values(array)剥离对象,但有没有办法用对象做到这一点?

感谢您的帮助。

12 个答案:

答案 0 :(得分:1)

您将遍历数组,检查目标对象的id属性,然后使用amount属性中存储的值枚举和外部范围变量。

var totalShirts = 0;
var totalCards = 0;
for(var i = 0, len = array.length; i < len; i++){
    var entry = array[i];
    if(entry.id === "cards"){
        totalCards += entry.amount;
    }
    else if(entry.id === "shirts"){
        totalShirts += entry.amount;
    }
}
console.log("Total Cards: " + totalCards);
console.log("Total Shirts: " + totalShirts);

答案 1 :(得分:1)

以下是获取每个项目总数的示例

&#13;
&#13;
var array = [{id:"cards", amount: 5}, {id:"shirts", amount: 3}, {id:"cards", amount: 2}, {id:"shirts", amount: 3}];

var result = array.reduce(function(accumulator, current) {
  if (!(current.id in accumulator)) {
    accumulator[current.id] = current.amount;
  } else {
    accumulator[current.id] += current.amount;
  }
  
  return accumulator;
}, {});

console.log(result);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

这应该做你想要的:

&#13;
&#13;
.bold {
  font-weight: bold;
}
&#13;
&#13;
&#13;

答案 3 :(得分:1)

一个简单的 ... <ItemsControl Grid.Row="1" ItemsSource="{Binding SelectedUnit.LocalControllerInfo.Inputs}"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Margin="10" FontSize="15" Style="{StaticResource TextBlockNormalBase}" Text="{Binding InputName}"/> <StackPanel Orientation="Horizontal"> <RadioButton Margin="10" Foreground="White" Content="Normal" IsChecked="{Binding Path=?, Converter={StaticResource inputToBoolConverter}, ConverterParameter=?}"/> <RadioButton Margin="10" Foreground="White" Content="Active" IsChecked="{Binding Path=?, Converter={StaticResource inputToBoolConverter}, ConverterParameter=?}"/> </StackPanel> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> ... 可以解决问题:

public class Unit : BindableObject
{
    public enum InputState
    {
        Normal,
        Active
    }

    private LocalControllerTypes.LocalController _localControllerInfo;
    private LocalControllerTypes.ArduinoInjector[] _arduinoInjector;
    private WebWriter.WebWriter[] _inputWriters;
    private SNMPNetworkSwitchConnection.SNMPNetworkSwitchConnection _networkSwitchConnection;
    private InputState[] _inputStates;
    private bool _isUnitConnected;

    public Unit(LocalControllerTypes.LocalController localControllerInfo,
        LocalControllerTypes.ArduinoInjector[] arduinoInjector,
        WebWriter.WebWriter[] inputWriters,
        SNMPNetworkSwitchConnection.SNMPNetworkSwitchConnection networkSwitchConnection)
    {
        _localControllerInfo = localControllerInfo;
        _arduinoInjector = arduinoInjector;
        _inputWriters = inputWriters;
        _networkSwitchConnection = networkSwitchConnection;
        // This assumption might not always be true, but there is no way for now to get the input state
        _inputStates = Enumerable.Repeat(InputState.Normal, _inputWriters.Length).ToArray();
        // This assumption might not always be true, but there is no way for now to get the connection state
        _isUnitConnected = true;
    }

    public LocalControllerTypes.LocalController LocalControllerInfo
    {
        get
        {
            return _localControllerInfo;
        }
        set
        {
            if (_localControllerInfo != value)
            {
                _localControllerInfo = value;
                RaisePropertyChanged();
            }
        }
    }

    public LocalControllerTypes.ArduinoInjector[] ArduinoInjectors
    {
        get
        {
            return _arduinoInjector;
        }
        set
        {
            if (_arduinoInjector != value)
            {
                _arduinoInjector = value;
                RaisePropertyChanged();
            }
        }
    }

    public WebWriter.WebWriter[] InputWriters
    {
        get
        {
            return _inputWriters;
        }
        set
        {
            if (_inputWriters != value)
            {
                _inputWriters = value;
                RaisePropertyChanged();
            }
        }
    }

    public SNMPNetworkSwitchConnection.SNMPNetworkSwitchConnection NetworkSwitchConnection
    {
        get
        {
            return _networkSwitchConnection;
        }
        set
        {
            if (_networkSwitchConnection != value)
            {
                _networkSwitchConnection = value;
                RaisePropertyChanged();
            }
        }
    }

    public InputState[] InputStates
    {
        get
        {
            return _inputStates;
        }
        set
        {
            if (_inputStates != value)
            {
                _inputStates = value;
                RaisePropertyChanged();
            }
        }
    }

    public bool IsUnitConnected
    {
        get
        {
            return _isUnitConnected;
        }
        set
        {
            if (_isUnitConnected != value)
            {
                _isUnitConnected = value;
                RaisePropertyChanged();
            }
        }
    }
}

将打印:

public class MainViewModel : INotifyPropertyChanged
{
    private Unit _selectedUnit;
    private ObservableCollection<Unit> _units;
    private string _reader1RawCardData;
    private string _reader2RawCardData;
    private int _reader1BitsCount;
    private int _reader2BitsCount;

    public event PropertyChangedEventHandler PropertyChanged;

    public MainViewModel(IUnitStore unitStore)
    {
        UnitStore = unitStore;

        // We could use directly the unitstore instead of creating another container and binding on that, but
        // not doing so will allow us to add unit filtering further down the road
        _units = new ObservableCollection<Unit>(unitStore.Units);
        _selectedUnit = _units.First();

        _reader1RawCardData = "";
        _reader2RawCardData = "";
        _reader1BitsCount = 0;
        _reader2BitsCount = 0;
    }

    protected void RaisePropertyChanged([CallerMemberName]string propertName = "")
    {
        var temp = PropertyChanged;
        if (temp != null)
        {
            temp(this, new PropertyChangedEventArgs(propertName));
        }
    }

    protected void RefreshUnitStore(object obj)
    {
        UnitStore.UpdateStore();
        Units = new ObservableCollection<Unit>(UnitStore.Units);
        SelectedUnit = Units.First();
    }

    protected void SendReaderCardSwipe(object obj)
    {
        int unitReaderNumber = (int)obj;
        IPAddress arduinoIp = SelectedUnit.LocalControllerInfo.Readers[unitReaderNumber - 1].InjectorIp;
        int injectorNumber = SelectedUnit.LocalControllerInfo.Readers[unitReaderNumber - 1].InjectorNumber;
        string serviceUrl = SelectedUnit.ArduinoInjectors.Where(injector => injector.Ip.Equals(arduinoIp)).First().Url;

        InjectorInterface.CardSwipe<IPAddress>(serviceUrl, arduinoIp, injectorNumber, Reader1BitsCount, Reader1RawCardData);
    }

    protected void UpdateSelectedUnitConnectionState(object obj)
    {
        ((INetworkConnection.INetworkConnection)SelectedUnit.NetworkSwitchConnection).SetConnection(SelectedUnit.IsUnitConnected);
    }

    public IUnitStore UnitStore
    {
        get;
        private set;
    }

    public Unit SelectedUnit
    {
        get
        {
            return _selectedUnit;
        }
        set
        {
            if (_selectedUnit != value)
            {
                _selectedUnit = value;
                RaisePropertyChanged();
            }
        }
    }

    public ObservableCollection<Unit> Units
    {
        get
        {
            return _units;
        }
        set
        {
            if (_units != value)
            {
                _units = value;
                RaisePropertyChanged();
            }
        }
    }

    public string Reader1RawCardData
    {
        get
        {
            return _reader1RawCardData;
        }
        set
        {
            if (_reader1RawCardData != value)
            {
                _reader1RawCardData = value;
                RaisePropertyChanged();
            }
        }
    }

    public string Reader2RawCardData
    {
        get
        {
            return _reader2RawCardData;
        }
        set
        {
            if (_reader2RawCardData != value)
            {
                _reader2RawCardData = value;
                RaisePropertyChanged();
            }
        }
    }

    public int Reader1BitsCount
    {
        get
        {
            return _reader1BitsCount;
        }
        set
        {
            if (_reader1BitsCount != value)
            {
                _reader1BitsCount = value;
                RaisePropertyChanged();
            }
        }
    }

    public int Reader2BitsCount
    {
        get
        {
            return _reader2BitsCount;
        }
        set
        {
            if (_reader2BitsCount != value)
            {
                _reader2BitsCount = value;
                RaisePropertyChanged();
            }
        }
    }

    public ICommand RefreshSourceCommand
    {
        get
        {
            return new RelayCommand(RefreshUnitStore);
        }
    }

    public ICommand SendReaderCardSwipeCommand
    {
        get
        {
            return new RelayCommand(SendReaderCardSwipe);
        }
    }

    public ICommand UpdateSelectedUnitConnectionStateCommand
    {
        get
        {
            return new RelayCommand(UpdateSelectedUnitConnectionState);
        }
    }
}

答案 4 :(得分:0)

您可以使用Array#reduce并将金额相加。

&#13;
&#13;
var array = [{ id: "cards", amount: 5 }, { id: "shirts", amount: 3 }, { id: "cards", amount: 2 }, { id: "shirts", amount: 3 }],
    result = array.reduce(function (r, a) {
        r[a.id] = (r[a.id] || 0) + a.amount;
        return r;
    }, {});
    
console.log(result);
&#13;
&#13;
&#13;

答案 5 :(得分:0)

这是一个O(n)时间解决方案。

var totals = new Object();

for(var i = 0;i < array.length;i ++) {
  var id = array[i].id;
  var amount = array[i].amount;
  if(totals[id] == undefined) {
    totals[id] = amount; 
  } else {
    totals[id] += amount;
  }
}
console.log(totals);

答案 6 :(得分:0)

您可以使用for..of循环

var array = [{
  id: "cards",
  amount: 5
}, {
  id: "shirts",
  amount: 3
}, {
  id: "cards",
  amount: 2
}, {
  id: "shirts",
  amount: 3
}]

let res = {};

for (let {id,amount} of array) {
  if (!res.hasOwnProperty(id)) res[id] = 0;
  res[id] += amount;
}

console.log(res);

答案 7 :(得分:0)

使用for循环执行此操作:

var totalCards = 0; 
var totalShirt = 0;
for (var i = 0; i < arr.length; i++) {
    if (arr[i].id === "cards") {
        totalCards += arr[i].amount;
    } else {
        totalShirt += arr[i].amount;
    }
}

答案 8 :(得分:0)

for for循环中的魔力。这个例子应该足够通用:

var array = [ {id:"cards", amount: 5}, {id:"shirts", amount: 3}, {id:"cards", amount: 2}, {id:"shirts", amount: 3} ];
var output = [];
    
for(var i of array) {
  if(!output[i.id]) {
    output[i.id] = 0;
  }
  output[i.id] += i.amount;
}
    
console.log(output);

答案 9 :(得分:0)

&#13;
&#13;
var array = [{id:"cards", amount: 5}, {id:"shirts", amount: 3}, {id:"cards", amount: 2}, {id:"shirts", amount: 3}];

var arr = [];
array.forEach(v => arr.push(v.id));
var newArr = [...new Set(arr)];
var arr2 = [];

newArr.forEach(function(v) {
  var obj = {};
  obj.id = v;
  obj.counter = 0;
  arr2.push(obj);
});

arr2.forEach(v => array.forEach(c => c.id == v.id ? v.counter += c.amount : v));
console.log(arr2);
&#13;
&#13;
&#13;

答案 10 :(得分:0)

您可以使用Array.forEach()迭代数组的每个元素。总对象是一个关联数组,其中索引是数组元素对象的id字段。

var array = [{ id: "cards", amount: 5 },
             { id: "shirts", amount: 3 },
             { id: "cards", amount: 2},
             { id: "shirts", amount: 3 }];
var total = {};
array.forEach(function (el) {
  if (total[el.id]) {
    total[el.id] += el.amount
  } else {
    total[el.id] = el.amount
  }
});
console.log(JSON.stringify(total));

答案 11 :(得分:-1)

您可以使用此代码

if (!Object.keys) {
    Object.keys = function (obj) {
        var keys = [],
            k;
        for (k in obj) {
            if (Object.prototype.hasOwnProperty.call(obj, k)) {
                keys.push(k);
            }
        }
        return keys;
    };
}

然后您也可以在旧浏览器中执行此操作:

var len = Object.keys(obj).length;