gpt4 book ai didi

javascript - Angular : How to concat two arrays?

转载 作者:行者123 更新时间:2023-11-30 17:28:09 25 4
gpt4 key购买 nike

我有以下带值的数组(我正在生成值)

$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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com