我有数据
var description="Name:John;EmployeeID:2;Salary:$8000;Address:London";
Run Code Online (Sandbox Code Playgroud)
我希望结果为
Name: John
Employee Id: 2
Salary: $8000
Address: London
Run Code Online (Sandbox Code Playgroud)
是否可以在javascript中使用split()函数?
我为我的应用程序使用jQuery,我有一个包含字符串项的数组:
var array = ["item","item1","item2","item3"]
如何测试数组是否包含"item"?
我会直截了当。我有两个数组:
fruits = ["Banana", "Apple"];
fruitsSpecified = ["Big Banana", "Banana Small", "Black Apple", "Big Orange"];
Run Code Online (Sandbox Code Playgroud)
我正在寻找的结果:
newArray = ["Big Banana", "Banana Small", "Black Apple"];
Run Code Online (Sandbox Code Playgroud)
因此,我想检查fruitsSpecified字符串是否包含的一部分fruits并将这些结果放入新数组中。我怎么做?我发现的所有答案都过于复杂,只寻找完全匹配的内容(例如,数组1中的“ Apple”和数组2中的“ Apple”)。
到目前为止,我有:
function testFunction() {
newArray = [];
for (i = 0; i < fruitsSpecified.length; i++) {
myResult = fruitsSpecified.indexOf(fruits[i]);
newArray.push(myResult);
}
console.log(newArray);
}
Run Code Online (Sandbox Code Playgroud)
由于只能找到完全匹配的内容,因此显然不起作用。
我已经检查了这些问题(但是发现它们太复杂了,我相信/希望有一个更简单的解决方案):
查找项目是否在JavaScript数组中的最佳方法?
如何检查数组是否在JavaScript中包含对象?
谢谢!
如何根据匹配某个变量的选项将"已选择"属性添加到下拉菜单选项中?
以下是填充下拉列表的方式......
loginOptions = ["First", "Second", "Third"];
var Login = $( '#Login' );
for ( var i = 0; i < loginOptions.length; i++ ) {
Login.append('<option value="'+ loginOptions[i] +'">'+ loginOptions[i] +'</option>');
}
Run Code Online (Sandbox Code Playgroud)
我想根据其值是否与另一个变量匹配,将一个选项标记为"已选择".所以if loginOption[i]等于var existingLoginValue然后将该选项设置为'selected'
可以做的事情
if(loginOptions[i] === existingLoginValue){ print 'selected'; };
Run Code Online (Sandbox Code Playgroud)
谢谢!
我有这样一个数组:
arr['key1'] = true;
arr['key2'] = true;
arr['key3'] = true;
...
arr['keyN'] = true;
Run Code Online (Sandbox Code Playgroud)
如何确定,有没有人键入"虚假"值?