简体   繁体   English

从数组创建JSON对象

[英]Create JSON object from array

I have an input field, #tag_field , where my users enter a tag, which I would then like to check if the specified value exists in the JSON object. 我有一个输入字段#tag_field ,我的用户在其中输入标签,然后我想检查该标签是否在JSON对象中存在。

Included below is the code that I'm currently using but am not sure how to pass the user input to the function on the blur() event. 下面包括的是我当前正在使用的代码,但是不确定如何将用户输入传递给blur()事件上的函数。

var tags = [
                    {
                        "tag": "php",
                        "id": "x&8j"
                    },
                    {
                        "tag": "ruby",
                        "id": "x&8jX90"
                    }
];

$j('#tag_field').blur(function(){
        var tagMap = {};
        var i = null;
        for (i = 0; tags.length > i; i += 1) {
            tagMap[tags[i].tagName] = tags[i];
        }

        var hasTag = function(tagName) {
            return tagMap[tagName];
        };
    });

Do you mean some like this 你的意思是这样

Do you just need to take the value of the input and compare it to the names in your tags object? 您是否只需要获取输入值并将其与标签对象中的名称进行比较?

$('#tag_field').blur(function() {
    var tagMap = {};
    var i = null;
    for (i = 0; tags.length > i; i += 1) {

        if (tags[i].tag == $(this).val()) alert('exist' + tags[i].id)
        //Check
    }

    var hasTag = function(tagName) {
        return tagMap[tagName];
    };
});​
var tags = [
    {
        "tag": "php",
        "id": "x&8j"
    },
    {
       "tag": "ruby",
       "id": "x&8jX90"
    }
]

$(function() { 
    $('#tag_field').on('blur', function(e) {
        var $this = $(this), val = $.trim($this.val());
        if (!val) return false;

        for(var i=0, l=tags.length;i<l;i++) {
            if (tags[i]['tag'] === val) {
                console.log('tag exists')
                return tags[i]['tag'];
            }
        }
        console.log('tag does not exist');
        return false;
    })
})

Simplifying your tag map (if possible) will make it easier still 简化标签图(如果可能)将使其更容易

var tags =  { "php" : "x&8j", "ruby" : "x&8jX90" }

$(function() { 
    $('#tag_field').on('blur', function(e) {
      if(tags[$.trim($(this).val())]) {
          console.log('tag exists')
      } else {
          console.log('tag does not exist')
      }
    })
})
​

You'll be best off using Array.some when available: 如果可用,最好使用Array.some

function tagExists(needle, haystack) {
  if (Array.prototype.some) {
    return haystack.some(function(element) {
      return element.tag == needle
    });
  } else {
    for(i in haystack) {
      if(typeof(haystack[i]) == 'object' && haystack[i].tag == needle) {
        return true;
      }
    }
    return false;
  }
}

To use it in your specific scenario, something along the lines of: 要在您的特定情况下使用它,应遵循以下步骤:

$('#tag_field').on('blur', function() {
  if(tagExists($(this).val(), tags) {
    // Tag exists. 
  }
});

Note that this is case-sensitive as I defined it but could be tweaked easily by calling String.toLowerCase() on the needle as well as each tag during the comparison. 请注意,这是我定义的区分大小写的代码,但是可以通过在needle以及比较期间的每个标签上调用String.toLowerCase()轻松进行调整。

Finally, perhaps I'm being over picky, but your tag map is not technically a JSON object; 最后,也许我已经太挑剔了,但是从技术上来说,您的标记图不是JSON对象; it is simply an array of objects. 它只是一个对象数组。

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

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