动态地我得到array
。
例如,我们可以考虑以下array
。
var sampleArray=[
"logo",
"Details",
"titles"
];
但我想要这样的东西。
jsonObj={
"poistion1":"logo",
"poistion2":"Details",
"poistion3":"titles"
}
答案 0 :(得分:4)
您可以迭代数组并创建对象,如下所示
<fieldType name="string_ci" class="solr.TextField" sortMissingLast="true" omitNorms="true" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
答案 1 :(得分:3)
您可以创建一个空对象,然后循环(Array.forEach())数组并指定值
var sampleArray = [
"logo",
"Details",
"titles"
];
var obj = {};
sampleArray.forEach(function(value, idx) {
obj['position' + (idx + 1)] = value
});
snippet.log(JSON.stringify(obj))
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
答案 2 :(得分:3)
喜欢这个
{{1}}&#13;
答案 3 :(得分:2)
var arr=[
"logo",
"Details",
"titles"
];
var result = {};
for (var i = 0; i < arr.length; ++i){
result["position" + (i+1)] = arr[i];
}
答案 4 :(得分:0)
试试这个
var obj = {};
var sampleArray=[
"logo",
"Details",
"titles"
];
for(var index in sampleArray) {
obj['pos' + index] = sampleArray[index];
}
答案 5 :(得分:0)
您可以使用JSON对象:
var yourObject = [123, "Hello World", {name: "Frankie", age: 15}];
var yourString = JSON.stringify(yourObject); // "[123,"Hello World",{"name":"Frankie","age":15}]"
JSON对象还具有JSON到对象功能:
var anotherObject = JSON.parse(yourString);