如何合并这些数组/json对象? [英] How to merge these arrays/json objects?

查看:111
本文介绍了如何合并这些数组/json对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一点上,我对什么是对象,什么是数组以及什么是JSON感到有些困惑.有人可以解释两者在语法上的差异吗?以及如何向每个项目添加项目,如何合并每种类型等等?我正在尝试获取此功能以从JSON对象中获取新信息(我认为)并将其与一些新信息合并.然后,这些信息将被传递到PHP脚本进行处理.

I am a bit confused at this point on what is an object, what is an array, and what is a JSON. Can someone explain the differences in syntax between the two? and how to add items to each, how to merge each type, and such? I am trying to get this function to take the new information from a JSON object (I think) and merge it with some new information. This information will then be passed to a PHP script to be processed.

这是控制台输出:

{"public":{"0":["el29t7","3bmGDy"]}} 
{"public":"[object Object][object Object]"} 

这是我正在使用的JS:

Here is the JS I am using:

/* Helper function to clean up any current data we have stored */
function insertSerializedData(ids, type) {
    // Get anything in the current field
    current_data = $('#changes').val();
    if (!current_data) {
        var data = {};
        data[index++] = ids;
        var final_data = {};
        final_data[type] = data;
        $('#changes').val(JSON.stringify(final_data));
    } else {
        current_data = JSON.parse(current_data);
        var data = {};
        data[index++] = ids;
        // Does the index exist?
        if (type in current_data) {
            var temp_data = current_data[type];
            current_data[type] = temp_data + data;
        } else {
            current_data[type] = data;
        }
        //var extra_data = {};
        //extra_data[type] = data;
        //$.merge(current_data, extra_data);
        $('#changes').val(JSON.stringify(current_data));
    }
    console.log($('#changes').val());
}

这个想法是,如果键(公共键或其他键)尚不存在,则使其指向数组数组.如果确实存在,则需要将数组的数组与新数组合并.例如:

The idea is if the key (public, or whatever other ones) doesn't exist yet, then to make it point to an array of arrays. If it does exist though, then that of array of arrays need to be merged with a new array. For instance:

如果我有

{"public":{"0":["el29t7","3bmGDy"]}}  

我想与

["aj19vA", "jO71Ba"] 

那么最终结果将是:

{"public":{"0":["el29t7","3bmGDy"], "1":["aj19vA", "jO71Ba"]}} 

我该怎么做?谢谢

推荐答案

优秀的两部分问题.总体而言,由于第一个问题的复杂性,第二个问题并不重要.

Excellent two-part question. Overall, the second question is non-trivial because of the complexity of the first.

问题1:

什么是对象,什么是数组,什么是JSON.有人可以吗 解释两者之间的语法差异?

what is an object, what is an array, and what is a JSON. Can someone explain the differences in syntax between the two?

问题2:

以及如何向其中添加项目

and how to add items to each,

问题3:

如何合并每种类型?

how to merge each type, and such?

答案1:

这是一个常见的绊脚石,因为JavaScript比最初预期的要灵活得多.这是曲线.

This is a common stumbling point because, JavaScript is more flexible than one might initially expect. Here is the curve.

在JavaScript中,一切都是对象.

In JavaScript everything is an object.

这是每个代码:

//What is an object?
var obj = { };                

var obj2 = { member:"value", myFunction:function(){} }

上面是一个空对象.然后是另一个具有变量和函数的对象. 它们被称为对象文字.

Above is an empty object. Then another object with a variable and a function. They are called object-literals.

//What is an array 
var array1 = [ ] ;     

var array2 = [0,1,2,3,4];

上面是一个空数组.然后是具有五个整数的另一个数组.

Above is an empty array. Then another array with five Integers.

这是引起混乱的曲线.

//Get elements from each of the prior examples.
var x = obj2["member"];
var y = array2[1];

什么??? Object和Array都使用方括号来访问值? 这是因为两者都是对象.事实证明,这是编写高级代码的一种很好的灵活性.数组是对象.

What??? Both Object and Array are accessing values with a bracket? This is because both are objects. This turns out to be a nice flexibility for writing advanced code. Arrays are objects.

//什么是JSON?

JSON代表JavaScript对象表示法.您可能已经猜到了.一切都是对象...它也是一个{ };,但是它有所不同,因为-它用于将数据从JavaScript传输到-和-,而不是在JavaScript中实际使用(通常)的.这是一种文件传输格式.

JSON stands for JavaScript Object Notiation. As you might have guessed. Everything is an object... It is also an { }; But it is different because - it is used to transfer data to - and - from JavaScript, not actually used (commonly) in JavaScript. It is a file transfer format.

var JSONObject = {"member":"value"};

与先前示例的唯一区别是引号.本质上,我们将对象文字包装为字符串,以便可以轻松地将其传送到服务器,也可以传送回服务器.比XML更好-因为不必自定义分析.只需调用stringify()ParseJSON().去谷歌上查询.关键是... JSON可以转换为对象文字的JS对象,而JS对象文字可以转换为JSON,例如,以传输到服务器或CouchDB数据库.

The only difference to the prior example is quotes. Essentially we are wrapping the object literal as a string so that it can be transferred to a server, or back, and it can be reinterpreted, very easily. Better than XML - because it does not have to be custom-parsed. Just call, stringify() or ParseJSON(). Google it. The point is... JSON can be converted into an object-literal JS object, and JS object-literals can be converted into JSON, for transfer to a server or a CouchDB database, for example.

很抱歉切线.

答案2:

如何为每个项目添加一个项目?在这里,曲线不再是令人讨厌的事情,而是变得令人敬畏!因为一切都是对象,所以几乎都是相同的.

How to add an item to each? Here is where the curve stops being a nuisance, and starts being awesome! Because everything is an object, it is all just about the same.

//Add to an object
var obj {member1:"stringvalue"}
obj.member2 = "addme";    //That is it!

//Add to an array
var array1 [1,2,3,4,5];
array1[0] = "addme";

array[6] = null;

//We shouldn't mix strings, integers, and nulls in arrays, but this isn't a best-practice tutorial.

记住JS对象的语法,您可能会开始看到一个全新的,灵活的对象世界.但这可能需要一点时间.

Remember the JS object syntax and you may start to see a whole new flexible world of objects open up. But it may take a bit.

答案3:是的,...如何合并.

Answer 3: Ah, yeah... how to merge.

有认真的(很多)合并两个数组的方法.这完全取决于您的需求.排序,重复,串联...有几个.

There are seriously (very many) ways to merge two arrays. It depends on exactly what you need. Sorted, Duplicated, Concatenated... there are a few.

这就是答案

更新:如何制作漂亮的多维数组.

UPDATE: How to make a beautiful multiple dimensional array.

//Multiple Dimension Array
var array1 = [1,2,3];
var array2 = [3,4];

var arraysinArray = [array1,array2]; //That is it!

这又是一条曲线,它可能在对象中:

Here is the curve again, this could be in an object:

var obj{
   array1:[1,2,3],
   array2:[3,4]
}

JavaScript是强大的工具,请坚持使用;它变得很好. :)

JavaScript is powerful stuff, stick with it; it gets good. : )

希望有帮助, 一切顺利! 纳什

Hope that helps, All the best! Nash

这篇关于如何合并这些数组/json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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