Javascript 如何在特定于数组的索引处插入数组,而不使用角点删除指定索引处的现有数据
我有一个数组,在这个数组中,如果满足条件,我想在特定索引上添加另一个数组 这是我的控制器:Javascript 如何在特定于数组的索引处插入数组,而不使用角点删除指定索引处的现有数据,javascript,arrays,angularjs,json,Javascript,Arrays,Angularjs,Json,我有一个数组,在这个数组中,如果满足条件,我想在特定索引上添加另一个数组 这是我的控制器: var app = angular.module('rulesApp'); app.controller('myController2', ['$scope', '$rootScope'] { $scope.rows = function() { $rootScope.rows.push({ id: $rootScope.input_columns.leng
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
}
}
});
var-app=angular.module('rulesApp');
app.controller('myController2',['$scope','$rootScope']{
$scope.rows=函数(){
$rootScope.rows.push({
id:$rootScope.input\u columns.length,
姓名:“姓名”
错,,
数据类型:“”,
键入:“输入”,
路径:“”
});//我需要在其中添加数组的Json
//一些代码
对于(var i=0;i
为初始对象附加图像
拼接后:
这里我有一个由两个元素组成的数组,我想在一个指定的索引上添加另一个数组,同时使用现有数据。为此,我尝试了许多解决方案,然后使用splice
尝试了splice,它删除了现有索引并添加了新索引。但我希望在指定的索引上可以添加数组,而不覆盖以前的数据。我尝试使用$rootScope.rows[i].push(cellValues)
但它给出了错误:未定义函数
如何在不覆盖现有数据的情况下在JSON中插入数组数据。为了更好地理解我面临的问题,我附上了两个屏幕截图
是否有任何方法可以在指定的索引处插入包含现有数据的cellValue
数组
这是在尝试切片代码后创建的结构。现在在1-3索引上创建单元格值数组,然后在第4和第5索引上创建以前的数据。但我想使用现有数据将cellValues
数据添加到指定索引本身
首先,我看到您的代码存在各种问题 拼接
rowValues=$rootScope.rows.slice(0,j).concat(cellValues, rowValues);
$rootScope.rows=$rootScope.rows.concat(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);
$rootScope.rows=$rootScope.rows.concat(cellValues);
在这里,您将删除所有数组项,直到索引j
。然后将这些项与单元格值合并,就像上面的示例一样,将返回字符串和对象的混合数组:
[["some", "value"], {...}, {...}]
解决方案
如果我理解您的意思是正确的,那么您希望将(concatate)合并到阵列。这可以按如下方式完成:
数组合并
rowValues=$rootScope.rows.slice(0,j).concat(cellValues, rowValues);
$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]
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
}
*/
//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]
对象合并
rowValues=$rootScope.rows.slice(0,j).concat(cellValues, rowValues);
$rootScope.rows=$rootScope.rows.concat(cellValues);
如果需要合并两个对象(关联数组),可以使用angular.extend(obj1,obj2)
范例
var rows=[1,2,3];
var cellValues=[4,5,6];
var newRow=rows.concat(cellValues);
console.log(newRow); //[1,2,3,4,5,6]
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
}
*/
//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]
更新
由于您有一个给定的索引(j
),因此必须使用该索引来标识行数组上的某个嵌套数组
范例
var rows=[1,2,3];
var cellValues=[4,5,6];
var newRow=rows.concat(cellValues);
console.log(newRow); //[1,2,3,4,5,6]
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
}
*/
//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;
}
for (var j = 0; j < $rootScope.input_columns.length; j++) {
for(var i=0;i<cellValues.length; i++) {
rows[j]["path"+i]=cellValues[i];
}
}
for(var j=0;j<$rootScope.input\u columns.length;j++){
行[j]。路径=单元格值;
}
选项2:
for (var j = 0; j < $rootScope.input_columns.length; j++) {
rows[j].paths=cellValues;
}
for (var j = 0; j < $rootScope.input_columns.length; j++) {
for(var i=0;i<cellValues.length; i++) {
rows[j]["path"+i]=cellValues[i];
}
}
for(var j=0;j<$rootScope.input\u columns.length;j++){
对于(var i=0;iangular中的数组,它们的行为是相同的。它们具有相同的原型方法,例如切片、拼接。在示例中,只需将数组设置为a、b、d,就可以了
a=[1,2,5],
b=[3,4],
INDEXWHERTOINSERT=2,//实际插入位置(索引+1)
d=[];
d=a.切片(索引WheretoInsert,a.长度)
警报(a.slice(0,indexWhereToInsert).concat(b,d))
我在$rootScope.rows内创建了一个数组,类似于:
$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
}
}
});
$scope.rows=函数(){
$rootScope.rows.push({
id:$rootScope.input\u columns.length,
姓名:“姓名”
错,,
数据类型:“”,
键入:“输入”,
路径:“”,
行值:[]
});//我需要在其中添加数组的Json
//一些代码
对于(var i=0;i
但在这种情况下,它将替换或添加特定索引上的新数据。我想用该索引上的现有数据添加新数据。如何实现这一点
嵌套循环的作用是什么?但我只想在特定索引处合并两个数组..就像在第二个循环中,j指定索引。因此在该索引上,我想添加cellVal具有现有值的ues我尝试了此解决方案,但给出了$rootScope.rows[j].concat不是一个函数错误,所以基本上您要做的是将一个对象与一个字符串数组合并,因为cellrows似乎是一个字符串数组,而rows是一个包含对象的数组。很难理解您真正想要实现的是什么?为什么不能将cellvalues数组仅仅作为对象的一个属性?您可以d还将每个值添加为对象的属性(路径1、路径2、…路径n)(不推荐)我尝试了您的解决方案,但它在0索引上插入了b,零索引处的值进入第一个索引。在我的情况下,b包含数组,因此假设它有4个元素,从0-3索引数组值开始,在第4个索引上插入前一个第0个索引值。我想在该索引上插入新数组,仅使用现有数据,您可以显示吗您是如何使用这段代码的?它是否发生在我的示例中?for(var i=0;i$rootScope.rows包含一个由两个元素组成的数组,如fi所示