简体   繁体   English

使用 Javascript(无 jquery)从 2 个 json 字符串追加数据 - 追加不是函数

[英]Using Javascript (no jquery) to append data from 2 json strings - append is not a function

I have formdata i stringify that looks like this我有 formdata 字符串化,看起来像这样

{"name":"jack miller", "address":"123 main st"} 

I want to append more records but i was getting "append is not a function"我想追加更多记录,但我得到“追加不是函数”

goal to have this {"name":"jack miller", "address":"123 main st"}, {"name":"new guy", "address":"new address"}目标 {"name":"jack miller", "address":"123 main st"}, {"name":"new guy", "address":"new address"}

const s = this hold master data
const data = new FormData(e.target);
let m = JSON.stringify(Object.fromEntries(data));

let c = s.concat(m)  // concat doesn't include a comma 
let d = s.append(m)  // append throw error of  append is not a function

So while i tend to have json strings, i could convert to object or array and then back if that is better or easier to append etc..因此,虽然我倾向于使用 json 字符串,但我可以转换为对象或数组,然后如果更好或更容易附加等,则返回。

Why doesn't append work?为什么附加不起作用?

Update: There is jsfiddle of the formdata https://jsfiddle.net/fx6ebaj5更新:有表单数据的jsfiddle https://jsfiddle.net/fx6ebaj5

That "m" is what i need to add data to.那个“m”是我需要添加数据的。

How to add objects to array如何将对象添加到数组

let arr = [{"name":"jack miller", "address":"123 main st"} 
];

without modifying actual array不修改实际数组

let obj = {"name":"John", "address":"432 main st"} 
let newArr = [...arr, obj];
console.log(newArr);

let appendArr=arr.concat(obj)
console.log(appendArr);

modified actual array修改实际数组

arr.push(obj)
console.log(arr)

There are two ways of copying an object in Javascript.在 Javascript 中有两种复制对象的方法。 One is shallow copying and another one is deep copying.一种是浅拷贝,另一种是深拷贝。 The best way to copy an object would be to deep copy the feature as it would not affect the changes made to the copied one.复制对象的最佳方法是深度复制该功能,因为它不会影响对复制的功能所做的更改。

JSON.stringify turns an object into a string. JSON.parse turns a string into an object.

Combining them can turn an object into a string, and then reverse the process to create a brand new data structure.将它们组合起来可以将一个对象变成一个字符串,然后反过来创建一个全新的数据结构。

nestedObject = [
 {"name":"jack miller", "address":"123 main st"},
 {"name":"jon", "address":"13 main st"}];

numbersCopy = JSON.parse(
JSON.stringify(nestedNumbers)
);

numbersCopy.push({"name":"Shawn", "address":"13 main st"});
console.log(nestedObject, numbersCopy);

[
 {"name":"jack miller", "address":"123 main st"},
 {"name":"jon", "address":"13 main st"}
];

[
 {"name":"jack miller", "address":"123 main st"},
 {"name":"jon", "address":"13 main st"},
 {"name":"Shawn", "address":"13 main st"}
];

// These two arrays are completely separate!

So in your case to convert m as per your requirement, I would do below steps因此,在您根据您的要求转换m的情况下,我将执行以下步骤

const m = {hello: "hello", hi: "hi"}
let mArray = []

for (let [key, value] of Object.entries(m)) {
  let obj = {}
  obj.key = value
  mArray.push(obj)
}

mArray.push({"name":"Shawn", "address":"13 main st"})

So I am converting your m object to an array.所以我将你的m对象转换为一个数组。 It should work now.它现在应该可以工作了。

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

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