作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下带值的数组(我正在生成值)
$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];
我想连接这两个对象以获得以下结果
[{Name:''},{ Key: '', Value: '' }]
笨蛋 link , 不知何故这也不起作用
当我点击添加行按钮时,它会为键和值文本框添加另一行,而不是名称,我可以添加 n 行,当我点击提交时,它应该将 kev 值对显示为
[{Name:''},{ Key: '', Value: '' },{ Key: '', Value: '' },{ Key: '', Value: '' }.....so on]
谢谢
最佳答案
不确定为什么要构建不匹配对象的数组。这在我看来是自找麻烦。我建议可能执行以下操作:
$scope.objects = [{Name: '', Elements: []}];
然后你就可以轻松管理多个拥有元素的对象:
(我使用下划线 http://underscorejs.org/ )
$scope.addElementToObject = function(objName, element){
_.where($scope.mergedArray, {Name: objName}).Elements.push(element);
};
然后您可以添加到该对象的元素列表中,而无需在每次使用时评估元素数组中的对象。
如果您仍然想要/需要合并不匹配对象的数组,则如下所示:
$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];
$scope.mergedArray = $scope.objectName.contact($scope.propertiesElement);
$scope.addElement = function(element){
$scope.mergedArray.push(element);
};
然后,在您的点击事件代码中:
$scope.addElement({ Key: 'someKey', Value: 'Some Value' });
希望对您有所帮助。
关于javascript - Angular : How to concat two arrays?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23815292/
我是一名优秀的程序员,十分优秀!