[英]JavaScript Split from comma separated to array list with values
What I'm trying to do is from a comma separated text (like this one): 我想做的是用逗号分隔的文本(例如这样):
hello,test,ciao
Get a javascript array with a predetermined value. 获取具有预定值的javascript数组。 I know how to split a comma-separated list, but I don't know how to add a value inside them.
我知道如何分割以逗号分隔的列表,但是我不知道如何在其中添加值。
Actual code: 实际代码:
<input onkeyup="test()" type="text" id="origin">
<div id="response">
</div>
function getValue(){
var returnV = $("#origin").val();
return returnV
}
function test(){
var origin = getValue();
var array = origin.split(',');
console.log(array)
}
["hello", "test", "ciao"]
{
"hello":"predetermined value",
"test":"predetermined value",
"ciao":"predetermined value",
}
I think this question is interesting because this way yo can, for example, create new configurations with a starter value and add custom confirgurations for each of them later. 我认为这个问题很有趣,因为例如,您可以使用入门值创建新配置,并在以后为每个配置添加自定义配置。 I know that the split part is already replied on stackoverflow, what I'm having trouble with is with adding the default values :), thank you very much in advance.
我知道分裂部分已经在stackoverflow上回复了,我遇到的麻烦是添加默认值:),在此先非常感谢。
Have you tried iterating over the array? 您是否尝试过遍历数组? Like this:
像这样:
function getValue(){
var returnV = $("#origin").val();
return returnV
}
function test(){
var origin = getValue();
var object = {}
var array = origin.split(',');
array.forEach((item) => {
object[item] = "predeterminated value"
}
console.log(object)
}
A couple notes: try to give more meaningful names to your variables; 有几点注意事项:尝试给变量赋予更有意义的名称; the word is "predetermined",
这个词是“预定的”,
Store your output to a variable: 将输出存储到变量:
var output=["hello", "test", "ciao"]
Then do something like this with it: 然后用它做这样的事情:
var newObject={};
for(var i in output){
var property=output[i];
newObject[property]='predeterminated value';
}
Example JSFiddle (open console to see result) 示例JSFiddle (打开控制台查看结果)
Split your string and then use Array.prototype.reduce
to build an object from it. 拆分您的字符串,然后使用
Array.prototype.reduce
从中构建对象。
var original = "hello,test,ciao"; var obj = original.split(',').reduce(function(p,c) { p[c] = "predeterminated value"; return p; }, {}); console.log(obj);
It's not exactly clear where predeterminated value
is supposed to come from. 尚不清楚确切的
predeterminated value
应该从何而来。 If it's the same for all properties of your object, then this will work as-is. 如果对象的所有属性都相同,则将按原样工作。 If it's not, you'll have to figure out how to select the right value.
如果不是,则必须弄清楚如何选择正确的值。
Hey you can follow the below code snippet this will help 嘿,您可以按照以下代码片段进行操作,这将有所帮助
function getValue(){ var returnV = 'hey,your,input,keys,goes,here'; return returnV; } function test(){ var origin = getValue(); var array = origin.split(','); var finalObj = {}; var defaultVal = 'pre-def'; for(var i=0;i<array.length;i++){ finalObj[array[i]] = defaultVal; } console.log("arry",array) console.log("converted",finalObj) } test();
didn't want to change your code so much. 不想这么多更改代码。 just have a look up to object creation.
只是看看对象创建。
function getValue(){ return document.getElementById("origin").value } function test(){ var obj = {} var origin = getValue(); origin.split(", ").forEach(function(e) { obj[e] = "predeterminated value";} ); console.log(obj); }
<input onkeyup="test()" type="text" id="origin"> <div id="response"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.