我有一个字符串,我想要打破并传递一半像函数中的参数。但是该函数将参数视为整个片段。例如:aaa,bbb,ccc
(3个参数)但它理解一个参数'aaa,bbb,ccc'
。我该怎么做?
var text = "aaa,bbb,ccc@dd,eee,fff"
var itens_menu = text.split('@');
var item_menu1 = itens_menu[0];
var item_menu2 = itens_menu[1];
item = Menu(itens_menu[0]);
item2 = Menu(itens_menu[1]);
function Menu()
{
var ItemText = arguments[0];
}
答案 0 :(得分:4)
.split()
每个值。但是你还需要向Menu传递正确的变量。你传递的是itens_menu[0]
,但你应该传递item_menu1
。
var text = "aaa,bbb,ccc@dd,eee,fff"
var itens_menu = text.split('@');
var item_menu1 = itens_menu[0].split(',');
var item_menu2 = itens_menu[1].split(',');
var item = Menu(item_menu1);
var item2 = Menu(item_menu2);
function Menu() {
var ItemText = arguments[0]; // this is the array
console.log(ItemText);
// if you are wanting to first value of the array, you would do
// console.log(ItemText[0]) which would be either aaa, or dd
}

apply
如果您希望将所有值作为单独参数传递给Menu
,则需要apply
将数组Menu
设置为var text = "aaa,bbb,ccc@dd,eee,fff"
var itens_menu = text.split('@');
var item_menu1 = itens_menu[0].split(',');
var item_menu2 = itens_menu[1].split(',');
var item = Menu.apply(null, item_menu1);
var item2 = Menu.apply(null, item_menu2);
function Menu() {
var ItemText = arguments[0];
console.log(ItemText);
}
。然后,当您引用arguments对象时,您将获得字符串值:
SELECT
itemId,
itemTitle,
isFirstRow(itemId) AS isFirst,
isLastRow(itemId) AS isLast
FROM myTable
ORDER BY itemId

答案 1 :(得分:1)
您应该再次应用.split
,并且您也可以将数组项作为单个参数传递:
var text = "aaa,bbb,ccc@dd,eee,fff"
var itens_menu = text.split('@');
var item_menu1 = itens_menu[0].split(',');
var item_menu2 = itens_menu[1].split(',');
var item = Menu.apply(null, item_menu1);
var item2 = Menu.apply(null, item_menu2);
function Menu() {
console.log(arguments); // you'll have all the items as a single argument
// so
console.log(arguments[0]);
//this will return aaa for "item_menu1" and dd for "item_menu2"
}
答案 2 :(得分:0)
var text = "aaa,bbb,ccc@dd,eee,fff"
var itens_menu = text.split(',');
item = Menu(itens_menu[0]);
item2 = Menu(itens_menu[1]);
function Menu()
{
var ItemText = arguments[0];
alert(ItemText);
}
答案 3 :(得分:0)
var
text = "aaa,bbb,ccc@dd,eee,fff",
items_menu = text.split('@'),
item_menu1 = items_menu[0],
item_menu2 = items_menu[1],
item = Menu.apply(null, item_menu1.split(',')),
item2 = Menu.apply(null, item_menu2.split(','));
function Menu(entry1, entry2, entry3) {
console.log('arguments[0] : ', arguments[0]);
console.log('arguments[1] : ', arguments[1]);
console.log('arguments[2] : ', arguments[2]);
return {
entry1: entry1,
entry2: entry2,
entry3: entry3
};
}
console.log('item : ', item);
console.log('item2 : ', item2);