[英]Push data into javascript array if 2 data are matching (AngularJS)
So, I am working on an array list using JavaScript(angular). 因此,我正在使用JavaScript(角度)处理数组列表。
There is a form for user to add some data and there is a button to insert it into a div below the form. 有一个供用户添加一些数据的表单,还有一个按钮可将其插入到表单下方的div中。 This div is an array.
这个div是一个数组。
The important part of this form is the request_area. 此表单的重要部分是request_area。 Every product has its request_area.
每个产品都有其request_area。 So instead of pushing a div with same request_area over and over, i want to "group" it.
因此,我不想一遍又一遍地推送具有相同request_area的div,而是希望对其进行“分组”。 So here is the step
所以这是步骤
1.Array empty, user add product, and then 1 div created with Request Area 1. 1.将数组清空,用户添加产品,然后使用请求区域1创建1个div。
2.Request Area 1 exists, user add another product with the same request area, so now Request Area 1 should have 2 products inside. 2.Request Area 1存在,用户添加具有相同请求区域的另一个产品,因此现在Request Area 1内应有2个产品。
3.If user input another product with different request_area, the array should consist of 2 objects now (Request area 1 and 2). 3.如果用户输入另一个具有不同request_area的产品,则该数组现在应包含2个对象(请求区域1和2)。
Here is my code 这是我的代码
vm.request_area = [];
function insertProduct(){
var data = {
code: vm.data.code,
qty: vm.data.qty,
area: vm.data.area,
description: vm.data.description,
uom: vm.data.uom,
index_number: vm.data.index_number
};
var exist = false;
var number;
for(var i = 0; i < vm.request_area.length; i++){
if(vm.data.area == vm.request_area[i].request_area){
exist = true;
number = i;
break;
}
}
if(exist){
vm.request_area[number].details.push({data});
}
else{
vm.request_area.push({
request_area: vm.data.area,
details: {data}
});
}
}
The code above is working well, except in adding product into the same request_area. 上面的代码运行良好,除了将产品添加到同一request_area中。 the following code gives me
以下代码给了我
if(exist){
vm.request_area[number].details.push({data});
}
vm.request_area[number].push is not a function
Please note that I can add products and everything is working fine. 请注意,我可以添加产品,并且一切正常。 only the add product into existing request_area is failed.
仅将产品添加到现有request_area中失败。
What am i missing here??? 我在这里想念什么??? thank you :)
谢谢 :)
You are essentially targeting something in your code that is not of type array and trying to push to it. 您实际上是在代码中定位非数组类型的对象,然后尝试将其推送。 vm.request_area is the array.
vm.request_area是数组。 vm.request_area[number] is an entry in the array and is therefore not an array (unless it is a nested array, but I'm guessing it's not...right?)
vm.request_area [number]是数组中的一个条目,因此不是数组(除非它是嵌套数组,但我猜它不是...对吧?)
If you anticipate multiple matches, you may want to create an array of objects. 如果预期有多个匹配项,则可能要创建一个对象数组。 This may be overkill but I'm not entirely sure of your use case.
这可能有点过分,但是我不能完全确定您的用例。
You have a for loop here: 您在这里有一个for循环:
for(var i = 0; i < vm.request_area.length; i++){
if(vm.data.area == vm.request_area[i].request_area){
exist = true;
number = i;
break;
}
}
Why not do your check within this? 为什么不在此范围内检查?
for(var i = 0; i < vm.request_area.length; i++){
if(vm.data.area == vm.request_area[i].request_area){
number = i;
vm.request_area[number] = {details: 'foo'}
}
}
This should create an array of number-keyed objects. 这将创建一个数字键对象数组。 IE:
IE:
[1: {details: 'abc'}, 2: {details: 'xyz'}]
vm.request_area.push({
request_area: vm.data.area,
details: {data}
});
It seems that you just push an object into vm.request_area, not an array. 看来您只是将对象推入vm.request_area而不是数组。 So vm.request_area[number] is a normal object instead of an array, which has no method like
push
. 因此vm.request_area [number]是普通对象,而不是数组,没有像
push
这样的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.