[英]How do I insert an array at an array specific index without removing existing data at specified index using angular
我有一个数组,在该数组中,如果满足条件,我想在特定索引上添加另一个数组。
这是我的控制器:
var app = angular.module('rulesApp');
app.controller('myController2', ['$scope', '$rootScope'] {
$scope.rows = function() {
$rootScope.rows.push({
id: $rootScope.input_columns.length,
name: 'Name'
dropped: false,
dataType: '',
type: 'input',
path: ''
}); //Json in which I need to add array
//some code
for (var i = 0; i < cellValues.length; i++) {
for (var j = 0; j < $rootScope.input_columns.length; j++) {
$rootScope.rows.splice(j, 0, cellValues); //I want to add
cellValues array to rows Json on specified index
}
}
});
附加初始对象的图像
拼接后:
在这里,我有一个包含两个元素的数组,我也想在具有现有数据的指定索引上添加另一个数组。 为此,我尝试了许多解决方案,然后尝试使用splice进行splice
这是删除现有索引并添加新索引。 但是我希望在指定的索引上可以添加数组而不覆盖先前的数据。 我尝试使用$rootScope.rows[i].push(cellValues)
但它给出了error: function not defined
。
如何在不覆盖现有数据的情况下在JSON中插入数组数据。 我附上了两个屏幕截图,以更好地了解我面临的问题
有什么办法可以在指定索引处插入具有现有数据的cellValue
数组
这是尝试切片代码后创建的结构。 现在,将在1-3索引上创建单元格值数组,然后在4th和5th索引上创建先前的数据。 但是我想将cellValues
数据与现有数据一起添加到指定的索引本身。
首先,我看到您的代码存在各种问题。
拼接
基本上,您在这里所做的就是将字符串数组添加到对象数组中。
$rootScope.rows=[{...}, {...}];
cellValues=["some", "value"];
$rootScope.rows.splice(j, 0, cellValues);
console.log($rootScope.rows) //[{...}, {...}, ["some", "value"]]
拼接
rowValues=$rootScope.rows.slice(0,j).concat(cellValues, rowValues);
在这里,您将删除所有直到索引j
数组项。 然后将它们与cellValues合并,就像上面的示例一样,它将返回由字符串和对象组成的混合数组:
[["some", "value"], {...}, {...}]
如果我理解正确,那么您想合并(合并)到数组。 可以像下面这样完成:
数组合并
$rootScope.rows=$rootScope.rows.concat(cellValues);
例
var rows=[1,2,3];
var cellValues=[4,5,6];
var newRow=rows.concat(cellValues);
console.log(newRow); //[1,2,3,4,5,6]
对象合并
如果需要合并两个对象(关联数组),则可以使用angular.extend(obj1, obj2)
例
var obj1={
one:1,
two:2,
three:3
}
var obj2={
four:4,
five:5,
six:6
}
var obj3=angular.extend(obj1, obj2);
console.log(obj3);
/*
{
one:1,
two:2,
three:3,
four:4,
five:5,
six:6
}
*/
由于您具有给定的索引( j
),因此必须使用该索引来标识rows数组上的某个嵌套数组。 然后,将合并的嵌套数组与新数组(cellValues)合并;
例
//nested arrays
var rows=[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16],
];
//index of the nested array to merge
var arrayIndex=1;
//new array to merge with nested array (rows[arrayIndex])
var valuesToAdd=[22,33,44];
//Reference the nested array and merge it with the new array.
rows[arrayIndex]=rows[arrayIndex].concat(valuesToAdd);
console.log(rows[arrayIndex]); //[5, 6, 7, 8, 22, 33, 44]
但是,您需要将cellvalues字符串数组中的值添加到rows数组中已标识的对象中。 这可以通过两种方式完成:1)将paths属性添加到标识的对象,或2)将每个字符串作为属性添加到标识的对象。
选项1:
for (var j = 0; j < $rootScope.input_columns.length; j++) {
rows[j].paths=cellValues;
}
选项2:
for (var j = 0; j < $rootScope.input_columns.length; j++) {
for(var i=0;i<cellValues.length; i++) {
rows[j]["path"+i]=cellValues[i];
}
}
角度数组的行为相同。 它们具有相同的原型方法,例如切片,拼接。 只需在示例中将数组设置为a,b,d,它将对您有用。
a = [1, 2, 5], b = [3, 4], indexWhereToInsert = 2, //postion where you want to insert (index +1 ) actually d = []; d = a.slice(indexWhereToInsert, a.length) alert(a.slice(0, indexWhereToInsert).concat(b, d))
我在$ rootScope.rows Json中创建了一个数组,如下所示:
$scope.rows = function() {
$rootScope.rows.push({
id: $rootScope.input_columns.length,
name: 'Name'
dropped: false,
dataType: '',
type: 'input',
path: '',
rowValue:[]
}); //Json in which I need to add array
//some code
for (var i = 0; i < cellValues.length; i++) {
for (var j = 0; j < $rootScope.input_columns.length; j++) {
$rootScope.rows[j].rowValue.push(cellValues); //I am able to get array inside an array at specified index
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.