简体   繁体   English

JavaScript从逗号分隔到带有值的数组列表

[英]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: 实际代码:

HTML HTML

  <input onkeyup="test()" type="text" id="origin">
  <div id="response">

  </div>

JAVASCRIPT: JAVASCRIPT:

function getValue(){
  var returnV = $("#origin").val();
  return returnV
}
function test(){
    var origin = getValue();
    var array = origin.split(',');
    console.log(array)
}

OUTPUT OUTPUT

["hello", "test", "ciao"]

WHAT I'M TRYING TO GET 我想得到什么

{
  "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.

相关问题 如何在javascript中将数组拆分为单引号和逗号分隔列表 - How to split an array into single quotes and comma separated list in javascript 从angularjs中的逗号分隔字符串列表中拆分和映射数组 - Split and map array from comma separated string list in angularjs 在JavaScript中按逗号分隔的字符串分割值 - Split values by comma separated string in javascript 分割逗号分隔的值并将其添加到数组 - Split comma separated values and add them to array 如何在 javascript 中拆分逗号分隔列表,但不要使用 null 值拆分引号内的任何内容 - How to split a comma separated list in javascript, but do not split anything inside quotes with null values 从optgroup将逗号分隔的值拆分为数组-javascript - Splitting comma separated values into array from optgroup - javascript 逗号分隔值:从字符串到对象到列表 - Comma separated values: from strings to objects to list 复选框值与逗号分隔的列表 - Comma separated list from checkbox values 如何从数组中返回以逗号分隔的值? - How to return values separated by comma from array? 为什么用javaScript中的split()将逗号分隔的字符串不能转换为数组? - Why is the comma-separated string not converting to an array using split() in javaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM