按钮点击时在JSON中存储值 [英] store value in JSON on button click

查看:93
本文介绍了按钮点击时在JSON中存储值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSON新手,我正尝试使用JSON保存数据。当我点击按钮时,我有一个按钮的元素列表,我希望按钮的相应值保存在JSON中。我也想比较JSON中已存在的标题。



Demo Here

解决方案

可以简单地使用作为循环来检查具有该标题的元素是否已经存在:

  function alreadyAdded(itemTitle){
for(var i = 0; i< objArray.length; i ++){
if(objArray [i] .title === itemTitle){
返回true;
}
}
返回false;
};

另外,您并未使用json对象,只是一个JavaScript数组。



演示


I am new in JSON, i am trying to save data using JSON. I have a list of element with some button when we click the button i want the corresponding value of button are save in JSON. I am also want to compare the title with already exists in JSON.

Demo Here

解决方案

You can simply use a for loop to check if the element with that title is already there:

function alreadyAdded(itemTitle) {
    for (var i = 0; i < objArray.length; i++) {
        if (objArray[i].title === itemTitle) {
            return true;
        }
    }
    return false;
};

Also, you are not using a json object, just a JavaScript array.

Demo

这篇关于按钮点击时在JSON中存储值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆