[英]How to set JavaScript object's array in elements attribute?
I have an array of object and I want to save it as attribute's value in an element. 我有一个对象数组,我想将其另存为元素中的属性值。 I write following code:
我写以下代码:
var objArr = [ { class: "level-0", style: undefined }, { class: "level-1", style: undefined }, { class: "level-2", style: undefined }, { class: "level-3", style: undefined } ]; $(".temp-div").attr('div-properties', objArr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="temp-div"> Temp div here </div>
which generate 产生
<div class="temp-div" div-properties="[object Object],[object Object],[object Object],[object Object]">
Temp div here
</div>
I am trying to get 我试图得到
<div class="temp-div" div-properties="[{class: "level-0",style: undefined},{class: "level-1",style: undefined},{class: "level-2",style: undefined},{class: "level-3",style: undefined}]">
Temp div here
</div>
as output. 作为输出。 How can I achieve this?
我该如何实现?
JSON.stringify
is probably what you're after (you're currently inadvertently using .toString()
). JSON.stringify
可能就是您想要的(您目前无意中使用.toString()
)。 JSON.stringify
will remove your undefined
pairs because they aren't valid JSON values, so there's some footwork done to preserve them artificially. JSON.stringify
将删除您的undefined
对,因为它们不是有效的JSON值,因此需要做一些工作来人为地保留它们。
var objArr = [ { class: "level-0", style: undefined }, { class: "level-1", style: undefined }, { class: "level-2", style: undefined }, { class: "level-3", style: undefined } ]; const stringified = JSON.stringify( objArr, (k, v) => v === undefined ? "undefined" : v ).replace(/"undefined"/g, "undefined"); $(".temp-div").attr('div-properties', stringified); console.log(stringified); // testing 1-2
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="temp-div"> Temp div here </div>
Consider replacing undefined with null
to produce a valid, parseable JSON string. 考虑将undefined替换为
null
以生成有效的可解析的JSON字符串。 Here's how you might go about reviving such a structure: 这是恢复这种结构的方法:
var objArr = [ { class: "level-0", style: undefined }, { class: "level-1", style: undefined }, { class: "level-2", style: undefined }, { class: "level-3", style: undefined } ]; const stringified = JSON.stringify( objArr, (k, v) => v === undefined ? null : v ); $(".temp-div").attr('div-properties', stringified); /* ... later on in your code ... */ const parsed = JSON.parse($('.temp-div').attr('div-properties')); parsed.forEach(e => Object.keys(e).forEach(f => e[f] = e[f] === null ? undefined : e[f]) ); console.log(parsed);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="temp-div"> Temp div here </div>
First off I would advise against this if this data is used elsewhere, there are far better data structures (like an Array of Objects). 首先,我建议如果在其他地方使用此数据,则建议使用更好的数据结构(例如对象数组)。 Having said that, an attribute is always a string, so you need to convert the data into a string with JSON.stringify() and then use
.data()
to create a data-*
attribute. 话虽如此,属性始终是字符串,因此您需要使用JSON.stringify()将数据转换为字符串,然后使用
.data()
创建data-*
属性。
var objArr = [{ class: "level-0", style: void 0 }, { class: "level-1", style: void 0 }, { class: "level-2", style: void 0 }, { class: "level-3", style: void 0 }]; $('.temp-div').data('prop', JSON.stringify(objArr)); console.log($('.temp-div').data('prop'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="temp-div"> Temp div here </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.