javascript 如何向 JSON 数组添加一个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6538214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:00:18  来源:igfitidea点击:

How do I add one single value to a JSON array?

javascriptjson

提问by Dewan159

I am kind of new to the world of interface, and i found JSON is amazing, so simple and easy to use. But using JS to handle it is pain !, there is no simple and direct way to push a value, check if it exists, search, .... nothing !

我对界面世界有点陌生,我发现 JSON 非常棒,如此简单易用。但是使用JS来处理它是痛苦的!,没有简单直接的方法来推送值,检查它是否存在,搜索,......什么都没有!

and i cannot simply add a one single value to the json array, i have this :

我不能简单地向 json 数组添加一个值,我有这个:

loadedRecords = {}

i want to do this :

我想做这个 :

loadedRecords.push('654654')
loadedRecords.push('11')
loadedRecords.push('3333')

Why this is so hard ???!!!

为什么这么难???!!!

回答by Alex Wayne

Because that's an object, not an array.

因为那是一个对象,而不是一个数组。

You want this:

你要这个:

var = loadedRecords = []
loadedRecords.push('1234');

Now to your points about JSON in JS:

现在谈谈您对 JS 中 JSON 的看法:

there is no simple and direct way to push a value

没有简单直接的方法来推送值

JSON is a data exchange format, if you are changing the data, then you will be dealing with native JS objects and arrays. And native JS objects have all kinds of ways to push values and manipulate themeselves.

JSON 是一种数据交换格式,如果您正在更改数据,那么您将处理本机 JS 对象和数组。并且原生的 JS 对象有各种各样的方式来推送值和操作自己。

check if it exists

检查它是否存在

This is easy. if (data.someKey) { doStuff() }will check for existence of a key.

这很简单。 if (data.someKey) { doStuff() }将检查密钥是否存在。

search

搜索

Again JSON decodes to arrays and objects, so you can walk the tree and find things like you could with any data structure.

JSON 再次解码为数组和对象,因此您可以遍历树并像使用任何数据结构一样查找内容。

nothing

没有什么

Everything. JSON just translates into native data structures for whatever language you are using. At the end of the day you have objects (or hashes/disctionaries), and arrays which hold numbers strings and booleans. This simplicity is why JSON is awesome. The "features" you seek are not part of JSON. They are part of the language you are using to parse JSON.

一切。JSON 只是转换为您使用的任何语言的本机数据结构。在一天结束时,您将拥有对象(或哈希/字典)和包含数字字符串和布尔值的数组。这种简单性就是 JSON 很棒的原因。您寻求的“功能”不是 JSON 的一部分。它们是您用来解析 JSON 的语言的一部分。

回答by Naftali aka Neal

Well .pushis an array function.

Well.push是一个数组函数。

You can add an array to ur object if you want:

如果需要,您可以向 ur 对象添加一个数组:

loadedRecords = { recs: [] };

loadedRecords.recs.push('654654');
loadedRecords.recs.push('11');
loadedRecords.recs.push('3333');

Which will result in:

这将导致:

loadedRecords = { recs: ['654654', '11', '3333'] };

回答by Krishnamoorthy Acharya

Simple way to push variable in JS for JSON format

在 JS 中为 JSON 格式推送变量的简单方法

var city="Mangalore";
var username="somename"

var dataVar = {"user": 0,
                "location": {
                    "state": "Karnataka",
                    "country": "India",  
                },
            }

            if (city) {
                dataVar['location']['city'] = city;
            }
            if (username) {
                dataVar['username'] = username;
            }

回答by Prusse

{}is not an array is an object literal, use loadedRecords = [];instead.

{}不是数组是对象字面量,请loadedRecords = [];改用。

回答by spender

If you want to push to an array, you need to create an array, not an object. Try:

如果要推送到数组,则需要创建数组,而不是对象。尝试:

loadedRecords = [] //note... square brackets
loadedRecords.push('654654')
loadedRecords.push('11')
loadedRecords.push('3333')

回答by batman

You can only push things on to an array, not a JSON object. Arrays are enclosed in square brackets:

您只能将内容推送到数组,而不是 JSON 对象。数组包含在方括号中:

var test = ['i','am','an','array'];

What you want to do is add new items to the object using setters:

您想要做的是使用setter向对象添加新项目:

var test = { };
test.sample = 'asdf';
test.value = 1245;

Now if you use a tool like FireBug to inspect this object, you can see it looks like:

现在,如果您使用 FireBug 之类的工具检查此对象,您会看到它如下所示:

test {
    sample = 'asdf,
    value = 1245
}

回答by Nate

Whats wrong with:

有什么问题:

var loadedRecords = [ '654654', '11', '333' ];