Nếu bạn có một chuỗi như foo: 1, bar: 2
, bạn có thể chuyển đổi nó thành OBJ hợp lệ với:
str
.split[',']
.map[x => x.split[':'].map[y => y.trim[]]]
.reduce[[a, x] => {
a[x[0]] = x[1];
return a;
}, {}];
Cảm ơn Niggler trong #JavaScript vì điều đó.
Cập nhật với giải thích:
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
Tài liệu MDN khó hiểu:
- //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
- //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Bản demo: //jsbin.com/hiduhijevu/edit?js,console
Function:
const str2obj = str => {
return str
.split[',']
.map[keyVal => {
return keyVal
.split[':']
.map[_ => _.trim[]]
}]
.reduce[[accumulator, currentValue] => {
accumulator[currentValue[0]] = currentValue[1]
return accumulator
}, {}]
}
console.log[str2obj['foo: 1, bar: 2']] // see? works!
Gaurav Singhal
Phát triển web phía trước
Giới thiệu
React hỗ trợ chuyển đổi tệp JSON từ các nguồn khác nhau như chuỗi, mảng và đối tượng và JavaScript cho phép bạn chuyển đổi chuỗi thành dữ liệu JSON. JSON có thể được sử dụng từ tệp cục bộ hoặc phản hồi API của máy chủ vì JSON hiện đang trở thành một cách tiếp cận được tiêu chuẩn hóa để truyền dữ liệu giữa máy khách và máy chủ.
Dữ liệu chuỗi có thể dễ dàng chuyển đổi thành JSON bằng hàm stringify[]
và cũng có thể được thực hiện bằng cách sử dụng ____10, chấp nhận biểu thức JavaScript mà bạn sẽ tìm hiểu trong hướng dẫn này.stringify[]
function, and also it can be done using
0, which accepts the JavaScript expression that you will learn about in this guide.const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
Chuyển đổi chuỗi thành JSON bằng JSON.Stringify []
Dữ liệu JSON chứa cặp giá trị khóa làm giá trị chuỗi; Do đó, đôi khi dữ liệu có thể ở định dạng chuỗi và cần gửi qua cuộc gọi API. Dữ liệu phải được định dạng để gửi dữ liệu yêu cầu từ máy khách đến máy chủ và phản hồi sẽ chủ yếu ở định dạng JSON, nhưng bạn có thể chuyển đổi chuỗi thành JSON bằng hàm stringify[]
. Cú pháp cơ bản của hàm stringify[]
như sau:stringify[]
. The basic syntax of the function stringify[]
is as
follows:
1JSON.stringify[source_of_data]
JSX
Nguồn dữ liệu có thể là bất cứ thứ gì giống như biến trạng thái, biến
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
3 với giá trị chuỗi, v.v.const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
3 variable with a string value, and so on.Hãy chạy qua một ví dụ dựa trên phản ứng đơn giản. Tạo một biến trạng thái vào thành phần như được trình bày dưới đây.
1constructor[props] {
2 super[props];
3 this.state = {
4 stringData: {
5 categories: [
6 {
7 name: "test 1",
8 department: "Information Technology"
9 },
10 {
11 name: "test 2",
12 department: "Computer Engineering"
13 },
14 {
15 name: "test 3",
16 department: "Information Technology"
17 }
18 ]
19 }
20 };
21}
JSX
Nguồn dữ liệu có thể là bất cứ thứ gì giống như biến trạng thái, biến
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
3 với giá trị chuỗi, v.v.const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
4 containing multiple objects with category name and department.Hãy chạy qua một ví dụ dựa trên phản ứng đơn giản. Tạo một biến trạng thái vào thành phần như được trình bày dưới đây.
1componentDidMount[] {
2 // Converting a string to JSON
3 let jsonData = JSON.stringify[this.state.stringData];
4 console.log[jsonData];
5}
JSX
Nguồn dữ liệu có thể là bất cứ thứ gì giống như biến trạng thái, biến
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
3 với giá trị chuỗi, v.v.const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
5, a state variable containing an array
of objects.Hãy chạy qua một ví dụ dựa trên phản ứng đơn giản. Tạo một biến trạng thái vào thành phần như được trình bày dưới đây.
Ở đây trong đối tượng trạng thái, có một mảng bổ sung được gọi là
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
4 chứa nhiều đối tượng có tên và bộ phận danh mục.stringify[]
function.Bạn cũng có thể sử dụng chuỗi đơn giản với một mảng và cả mảng các đối tượng hoặc đối tượng số ít. Bước tiếp theo là chuyển đổi đối tượng thành JSON, như hình dưới đây.
Trong các dòng mã trên, nguồn dữ liệu là
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
5, một biến trạng thái chứa một mảng các đối tượng.const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
0 function in JavaScript is used to take an expression and return the string. As a result, it can be used to convert the string into JSON.Khi bạn mở bảng điều khiển trình duyệt, đầu ra sẽ trông như thế này.
0, and even if you pass multiple statements as an expression, the result will still work.
The simple syntax for using const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
0 is as follows:const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
Mảng các đối tượng hiện được chuyển đổi thành JSON và nó có thể được gửi đến máy chủ để chuyển yêu cầu dưới dạng định dạng tệp JSON bằng hàm stringify[]
.
1constructor[props] {
2 super[props];
3 this.state = {
4 stringData: {
5 categories: [
6 {
7 name: "test 1",
8 department: "Information Technology"
9 },
10 {
11 name: "test 2",
12 department: "Computer Engineering"
13 },
14 {
15 name: "test 3",
16 department: "Information Technology"
17 }
18 ]
19 }
20 };
21}
JSX
Nguồn dữ liệu có thể là bất cứ thứ gì giống như biến trạng thái, biến
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
3 với giá trị chuỗi, v.v.const str2obj = str => {
return str
.split[',']
.map[keyVal => {
return keyVal
.split[':']
.map[_ => _.trim[]]
}]
.reduce[[accumulator, currentValue] => {
accumulator[currentValue[0]] = currentValue[1]
return accumulator
}, {}]
}
console.log[str2obj['foo: 1, bar: 2']] // see? works!
0 as an argument to the const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
0, as shown below.1componentDidMount[] {
2 let evalData = eval[this.state.stringData];
3 console.log[evalData];
4}
JSX
Nguồn dữ liệu có thể là bất cứ thứ gì giống như biến trạng thái, biến
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
3 với giá trị chuỗi, v.v.const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
0 function followed by the state variable as an argument, const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
5, which converts the data into the specific format.Hãy chạy qua một ví dụ dựa trên phản ứng đơn giản. Tạo một biến trạng thái vào thành phần như được trình bày dưới đây.
Ở đây trong đối tượng trạng thái, có một mảng bổ sung được gọi là
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
4 chứa nhiều đối tượng có tên và bộ phận danh mục.const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
4, which is
the key of an array and has three different objects along with it.Bạn cũng có thể sử dụng chuỗi đơn giản với một mảng và cả mảng các đối tượng hoặc đối tượng số ít. Bước tiếp theo là chuyển đổi đối tượng thành JSON, như hình dưới đây. According to the official guidelines from //developer.mozilla.org/ ,
0 is a dangerous function that executes the code its passed with the caller's privileges. If you runconst obj = 'foo: 1, bar: 2' .split[','] // split into ['foo: 1', 'bar: 2'] .map[keyVal => { // go over each keyVal value in that array return keyVal .split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2'] .map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this }] .reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object accumulator[currentValue[0]] = currentValue[1] // accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it // since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[], // first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1'] // so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it // second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'} return accumulator }, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff console.log[obj]
0 with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage/extension. So it is not entirely suitable to use theconst obj = 'foo: 1, bar: 2' .split[','] // split into ['foo: 1', 'bar: 2'] .map[keyVal => { // go over each keyVal value in that array return keyVal .split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2'] .map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this }] .reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object accumulator[currentValue[0]] = currentValue[1] // accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it // since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[], // first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1'] // so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it // second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'} return accumulator }, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff console.log[obj]
0 function to evaluate the various JavaScript expression as a string because it attracts the malicious activity with the unsecured data.const obj = 'foo: 1, bar: 2' .split[','] // split into ['foo: 1', 'bar: 2'] .map[keyVal => { // go over each keyVal value in that array return keyVal .split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2'] .map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this }] .reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object accumulator[currentValue[0]] = currentValue[1] // accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it // since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[], // first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1'] // so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it // second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'} return accumulator }, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff console.log[obj]
Sự kết luận
Cả hai chức năng được đề cập trong hướng dẫn này, stringify[]
và
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
0, đều phù hợp để chuyển đổi giá trị chuỗi thành JSON, nhưng const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
0 biến không được bảo đảm để sử dụng; Do đó stringify[]
là cách tiếp cận tốt nhất. Bạn có thể thử 1JSON.stringify[source_of_data]
2 để lấy dữ liệu JSON và stringify[]
để chuyển đổi chuỗi thành JSON. Tôi hy vọng hướng dẫn này cung cấp đủ thông tin cho bạn để chuyển đổi một chuỗi thành JSON.stringify[]
and
const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
0, are suitable to convert a string value into JSON, but const obj = 'foo: 1, bar: 2'
.split[','] // split into ['foo: 1', 'bar: 2']
.map[keyVal => { // go over each keyVal value in that array
return keyVal
.split[':'] // split into ['foo', '1'] and on the next loop ['bar', '2']
.map[_ => _.trim[]] // loop over each value in each array and make sure it doesn't have trailing whitespace, the _ is irrelavent because i'm too lazy to think of a good var name for this
}]
.reduce[[accumulator, currentValue] => { // reduce[] takes a func and a beginning object, we're making a fresh object
accumulator[currentValue[0]] = currentValue[1]
// accumulator starts at the beginning obj, in our case {}, and "accumulates" values to it
// since reduce[] works like map[] in the sense it iterates over an array, and it can be chained upon things like map[],
// first time through it would say "okay accumulator, accumulate currentValue[0] [which is 'foo'] = currentValue[1] [which is '1']
// so first time reduce runs, it starts with empty object {} and assigns {foo: '1'} to it
// second time through, it "accumulates" {bar: '2'} to it. so now we have {foo: '1', bar: '2'}
return accumulator
}, {}] // when there are no more things in the array to iterate over, it returns the accumulated stuff
console.log[obj]
0 turns to be unsecured to use; hence stringify[]
is the best approach. You can try 1JSON.stringify[source_of_data]
2 to get JSON data and stringify[]
to convert a string to JSON. I hope this guide provides sufficient information to you for converting a string to JSON.Tìm hiểu thêm
Khám phá các khóa học JavaScript này từ Pluralsight để tiếp tục học: