我有两个要在GAS中合并的数组,arr2是多维的。
arr1 = ["Diesel", "Solar", "Biomass"]
arr2 = [
["ABC", "Nigeria", "Diesel,Solar", 35],
["DEF", "Egypt", "Solar,Diesel", 50],
["GHI", "Ghana", "Biomass,Diesel", 70]
]
Run Code Online (Sandbox Code Playgroud)
我想做的是将arr1的元素推入每行索引3的arr2中,因此它看起来像:
newArr = [
["ABC", "Nigeria", "Diesel,Solar", "Diesel", 35],
["DEF", "Egypt", "Solar,Diesel", "Solar", 50],
["GHI", "Ghana", "Biomass,Diesel", "Biomass", 70]
]
Run Code Online (Sandbox Code Playgroud)
我试图在arr2上使用.map来.Splice每行,但无法正常工作。任何帮助将非常感激!
我有一个如下的JavaScript数组:
[
{type: 'text', name: 'title', id: 'title', placeholder: 'Type here'},
{type: 'textarea', name: 'description', id: 'description', placeholder: 'Type here'}
]
Run Code Online (Sandbox Code Playgroud)
现在我想插入{type: 'text', name: 'age', id: 'age', placeholder: 'Type here'}第一个对象之后.所以我的最终结果集如下:
[
{type: 'text', name: 'title', id: 'title', placeholder: 'Type here'},
{type: 'text', name: 'age', id: 'age', placeholder: 'Type here'}
{type: 'textarea', name: 'description', id: 'description', placeholder: 'Type here'}
]
Run Code Online (Sandbox Code Playgroud)
我想用简单的JavaScript或jQuery!
如何在数组中的每x个项目中添加一个项目?例如,我想在第3个位置每10个项目添加一个项目:
const arr = [1,2];
const result = [1,2, item];
Run Code Online (Sandbox Code Playgroud)
要么
const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
const result = [1,2,item,3,4,5,6,7,8,9,10,11,item,12,13,14,15,16];
Run Code Online (Sandbox Code Playgroud) 我需要在打字稿对象数组中添加值
示例如果数组是
[
{'title':'this is post title1','content':'this is post content1'},
{'title':'this is post title2','content':'this is post content2'},
{'title':'this is post title3','content':'this is post content3'},
{'title':'this is post title4','content':'this is post content4'},
{'title':'this is post title5','content':'this is post content5'},
]
Run Code Online (Sandbox Code Playgroud)
我希望当我将新项目放入该数组的第一个时,就像 jQuery 中的 prepend 一样
[
{'title':'this is new item title','content':'this is new item content'},
{'title':'this is post title1','content':'this is post content1'},
{'title':'this is post title2','content':'this is post content2'},
{'title':'this is post title3','content':'this is post content3'},
{'title':'this is post title4','content':'this is …Run Code Online (Sandbox Code Playgroud) 我有数组 arrayData = ['abc', 'bcd', 'cdf', 'dfg']
有没有最快的方法将项目移动到数组的顶部?
所以最后的结果应该是 arrayData = ['abc', 'dfg' , 'bcd', 'cdf' ]
我有一个带有反向X / Y轴的条形图。这是我的代码和输出:
var myData = {};
myData.x = 'x';
myData.xFormat = "%Y-%m-%d";
myData.type = 'bar';
myX = app.reportsCollection.countedByDay.plotData.X;
myY = app.reportsCollection.countedByDay.plotData.Y;
myX.splice(0,0,'x');
myY.splice(0,0,'Nuevos Reportes');
myData.columns = [];
myData.columns.push(myX);
//http://stackoverflow.com/a/586189/1862909
myData.columns.push(myY);
var chart = c3.generate({
bindto: elementID,
data: myData,
size: {
height: 300
},
bar: {
width: {
ratio: 0.5 // this makes bar width 50% of length between ticks
}
// or
//width: 100 // this makes bar width 100px
},
axis: {
x: {
type: 'timeseries',
tick: { …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的数组:
var array = [3,5,6,2,1];
Run Code Online (Sandbox Code Playgroud)
我想插入7第一个位置而不删除,3所以结果数组将是这样的:
array = [7,3,5,6,2,1];
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一目标有什么建议吗?
我想将一个项目插入到空数组的指定索引中。我看到有Array.prototype.splice方法。但是,如果我在空数组上使用拼接,则只需添加项即可结束数组,如下所示。
var a = [];
a.splice(3,0,"item-3");
console.log(a); //returns ['item-3']
Run Code Online (Sandbox Code Playgroud)
我要完成的是具有如下所示的数组。
console.log(a); //returns [,,,'item-3']
or
console.log(a); //returns [undefined,undefined,undefined,'item-3']
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助。
编辑:我看到了有关如何将项目插入到特定索引的数组中的问题?但是,它没有解释如何插入到空数组的指定索引。
我想问你如何添加项目到集合,但在其他项目之间.例如.我有一个todoItem {name:todoItem2,date:2012-11-24},我想在集合中的其他项目之间添加它(beodween todoItem1和todoItem3之后不是todoItem3之后).
现在我添加项目如下:
items.push(form_Data);
this.collection.add(new itemM(form_Data));
有什么简单的方法吗?
我刚刚完成了section1,并注意到没有介绍如何将项目推到数组的特定位置的方法。例如,如果我想显示数组
var suits = ["hearts","clubs","Brooks Brothers", "diamonds","spades"]
Run Code Online (Sandbox Code Playgroud)
如何将“ Brooks Brothers”推入阵列服的位置[2],并将其余的1向下移?javascript中是否有类似于push的内置函数,使我能够做到这一点?
我想我总是可以有些困难:
function add (item, position){
var length = suits.length;
for(i = length -1; i >= position; i--){
suits[length] = suits[i];
length--;
};
suits[position] = item;
};
add("Brooks Brothers",2) //to add it to the middle
Run Code Online (Sandbox Code Playgroud) $('.creditCardText').keyup(function() {
var foo = $(this).val().split("-").join(""); // remove hyphens
if (foo.length > 0) {
foo = foo.match(new RegExp('.{1,4}', 'g')).join("-");
}
$(this).val(foo);
});
Run Code Online (Sandbox Code Playgroud)
我发现这个教程将破折号每4个字符之后,从这里我的问题是什么,如果字符间隔不固定就像这个例子只每4什么如果间隔之后是3 characters "-" 2 characters "-" 4 characters "-" 3 characters "-"这样它会出现这样的123-12-1234-123-123.
javascript ×10
arrays ×5
jquery ×2
backbone.js ×1
c3.js ×1
indexing ×1
insert ×1
regex ×1
typescript ×1