Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

javascript - 使用Javascript(无jquery)从2个json字符串追加数据-append不是函数(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?

(为什么不附加工作?)

  ask by Jeremy Miller translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...