简体   繁体   English

我该如何附加JavaScript?

[英]How can i append in javascript?

First off i'll state the obvious, i'm new to javascript,html, and web development tools. 首先,我将说明显而易见的一点,我是javascript,html和Web开发工具的新手。

var labels = {{ labels|tojson|safe }};

using console.log i was able to see the content of labels console.log(JSON.stringify(labels)); 使用console.log我能够看到标签的内容console.log(JSON.stringify(labels)); output: 输出:

[
    {"id":"1","image":"1-0.png","name":"","xMax":"4802","xMin":"4770","yMax":"156","yMin":"141"},
    {"id":"2","image":"1-0.png","name":"","xMax":"4895","xMin":"4810","yMax":"157","yMin":"141"},
    {"id":"3","image":"1-0.png","name":"","xMax":"4923","xMin":"4903","yMax":"156","yMin":"145"},
    {"id":"4","image":"1-0.png","name":"","xMax":"4956","xMin":"4931","yMax":"156","yMin":"145"}
]

It's clearly a list of dictionaries, so far so good, now there is new elements i want to append as it runs and WITHOUT REFRESHING THE PAGE Manually, it should auto update, so since i have some experience with python, i quickly thought about appending, something like that : 到目前为止,它显然是词典列表,到目前为止,现在我想在运行时添加新元素,并且无需手动刷新页面,它应该自动更新,因此由于我对python有一定的经验,因此我很快想到了添加,就像这样:

labels.append({"id":labels.length + 1, 
               "name":"",
               "xMin":xMin,
               "xMax":xMax,
               "yMin":yMin,
               "yMax":yMax
              })

but it isn't working, how can i append that line? 但它不起作用,我如何追加该行?

labels is a array type as it has labels是数组类型

[{"id":"1","image":"1-0.png","name":"","xMax":"4802","xMin":"4770","yMax":"156","yMin":"141"},{"id":"2","image":"1-0.png","name":"","xMax":"4895","xMin":"4810","yMax":"157","yMin":"141"},{"id":"3","image":"1-0.png","name":"","xMax":"4923","xMin":"4903","yMax":"156","yMin":"145"},{"id":"4","image":"1-0.png","name":"","xMax":"4956","xMin":"4931","yMax":"156","yMin":"145"}]

So you need to use labels.push(obj) to add new object in the labels instead of append() 因此,您需要使用labels.push(obj)labels添加新对象,而不是append()

 var labels = [{"id":"1","image":"1-0.png","name":"","xMax":"4802","xMin":"4770","yMax":"156","yMin":"141"},{"id":"2","image":"1-0.png","name":"","xMax":"4895","xMin":"4810","yMax":"157","yMin":"141"},{"id":"3","image":"1-0.png","name":"","xMax":"4923","xMin":"4903","yMax":"156","yMin":"145"},{"id":"4","image":"1-0.png","name":"","xMax":"4956","xMin":"4931","yMax":"156","yMin":"145"}] var xMin = 1, xMax = 5, yMin = 3, yMax = 8; labels.push({"id":labels.length + 1, "name":"", "xMin": xMin, "xMax":xMax, "yMin":yMin, "yMax":yMax}); console.log(labels); 

Array.prototype.append() does not exist. Array.prototype.append()不存在。 You need to use Array.prototype.push() if you want to add to it: 如果要添加到它,则需要使用Array.prototype.push()

Like this: 像这样:

 var labels = [{"id":"1","image":"1-0.png","name":"","xMax":"4802","xMin":"4770","yMax":"156","yMin":"141"},{"id":"2","image":"1-0.png","name":"","xMax":"4895","xMin":"4810","yMax":"157","yMin":"141"},{"id":"3","image":"1-0.png","name":"","xMax":"4923","xMin":"4903","yMax":"156","yMin":"145"},{"id":"4","image":"1-0.png","name":"","xMax":"4956","xMin":"4931","yMax":"156","yMin":"145"}]; console.log(labels); labels.push({"id":labels.length + 1, "name":"name", "xMin":'xMin', "xMax":'xMax', "yMin":'yMin', "yMax":'yMax'}); console.log(labels); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM