Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于Javascript中的列表从Json提取字段_Javascript_Json - Fatal编程技术网

基于Javascript中的列表从Json提取字段

基于Javascript中的列表从Json提取字段,javascript,json,Javascript,Json,我试图从Json响应中提取一些字段,并将它们推送到Javascript数组中。我希望字段的选择是可配置的。以下是我正在做的: 将其视为我的JSON字符串: { "id" : "1234", "orderNumber" : "1196", "createdOn" : "2019-07-02T12:03:39.697Z", "modifiedOn" : "2019-07-02T12:25:52.126Z", "testmode" : false, "c

我试图从Json响应中提取一些字段,并将它们推送到Javascript数组中。我希望字段的选择是可配置的。以下是我正在做的:

将其视为我的JSON字符串:

{
    "id" : "1234",
    "orderNumber" : "1196",
    "createdOn" : "2019-07-02T12:03:39.697Z",
    "modifiedOn" : "2019-07-02T12:25:52.126Z",
    "testmode" : false,
    "customerEmail" : "a@b.com",
    "billingAddress" : {
      "firstName" : "John",
      "lastName" : "Doe",
      "address1" : "Wall Street",
      "address2" : null,
      "city" : "NYC",
      "state" : "NY",
      "countryCode" : "US",
      "postalCode" : "12345",
      "phone" : "1122334455"
    }
}
假设我想提取一些字段(在fields变量中定义)并将它们放入数组中

# NOTE: the variable `data` here is my json object 

var FIELDS = ['id', 'orderNumber', 'customerEmail', 'billingAddress.firstName', 'billingAddress.lastName']

var lineItem = []

# parse the data (my Json object)
for (var i = 0; i < FIELDS.length; i++){
  lineItem.push(data[FIELDS[i]])
}
#注意:这里的变量'data'是我的json对象
变量字段=['id'、'orderNumber'、'customerEmail'、'billingAddress.firstName'、'billingAddress.lastName']
var lineItem=[]
#解析数据(我的Json对象)
对于(变量i=0;i
因此,对于第一级(id、订单号和customerEmail),这似乎可以正常工作,但对于billingAddress.firstname等,则不行。这就是我想知道的

我的目的是能够修改FIELDS变量中的定义,而不需要对代码中的逻辑进行任何更改

希望这是有意义的。
谢谢

只要键名中没有句点,这将起作用

var数据={
“id”:“1234”,
“订单号”:“1196”,
“createdOn”:“2019-07-02T12:03:39.697Z”,
“modifiedOn”:“2019-07-02T12:25:52.126Z”,
“testmode”:false,
“客户邮件”:a@b.com",
“billingAddress”:{
“名字”:“约翰”,
“姓氏”:“能源部”,
“地址1”:“华尔街”,
“地址2”:空,
“城市”:“纽约市”,
“州”:“纽约”,
“国家代码”:“美国”,
“postalCode”:“12345”,
“电话”:“1122334455”
}
};
变量字段=['id'、'orderNumber'、'customerEmail'、'billingAddress.firstName'、'billingAddress.lastName'];
var lineItem=[];
对于(变量i=0;iconsole.log(行项)只要键名中没有句点,这将起作用

var数据={
“id”:“1234”,
“订单号”:“1196”,
“createdOn”:“2019-07-02T12:03:39.697Z”,
“modifiedOn”:“2019-07-02T12:25:52.126Z”,
“testmode”:false,
“客户邮件”:a@b.com",
“billingAddress”:{
“名字”:“约翰”,
“姓氏”:“能源部”,
“地址1”:“华尔街”,
“地址2”:空,
“城市”:“纽约市”,
“州”:“纽约”,
“国家代码”:“美国”,
“postalCode”:“12345”,
“电话”:“1122334455”
}
};
变量字段=['id'、'orderNumber'、'customerEmail'、'billingAddress.firstName'、'billingAddress.lastName'];
var lineItem=[];
对于(变量i=0;iconsole.log(行项)
您只需要一个函数,该函数将在
上分割该路径并遍历JSON数据。这样,您就可以在字段上映射()

let data={“id”:“1234”,“orderNumber”:“1196”,“createdOn”:“2019-07-02T12:03:39.697Z”,“modifiedOn”:“2019-07-02T12:25:52.126Z”,“testmode”:false,“customerEmail”:a@b.com,“billingAddress”:{“firstName”:“John”,“lastName”:“Doe”,“address1”:“Wall Street”,“address2”:null,“city”:“NYC”,“state”:“NY”,“countryCode”:“US”,“postalCode”:“12345”,“phone”:“1122334455”}
变量字段=['id'、'orderNumber'、'customerEmail'、'billingAddress.firstName'、'billingAddress.lastName']
//分割导线
常量getFromPath=(路径,数据)=>path.split('.'))
.reduce((curr,p)=>curr&&curr[p],data)//在未定义路径的情况下检查curr

console.log(FIELDS.map(p=>getFromPath(p,data))
您只需要一个函数,该函数将在
上分割该路径并遍历JSON数据。使用该函数,您可以在字段上执行
map()

let data={“id”:“1234”,“orderNumber”:“1196”,“createdOn”:“2019-07-02T12:03:39.697Z”,“modifiedOn”:“2019-07-02T12:25:52.126Z”,“testmode”:false,“customerEmail”:a@b.com“,“billingAddress”:{“firstName”:“John”,“lastName”:“Doe”,“address1”:“Wall Street”,“address2”:null,“city”:“NYC”,“state”:“NY”,“countryCode”:“US”邮政编码:“12345”,“电话”:“1122334455”}
变量字段=['id'、'orderNumber'、'customerEmail'、'billingAddress.firstName'、'billingAddress.lastName']
//分割导线
常量getFromPath=(路径,数据)=>path.split('.'))
.reduce((curr,p)=>curr&&curr[p],data)//在未定义路径的情况下检查curr

log(FIELDS.map(p=>getFromPath(p,data)))
您如何从JSON对象创建
字段
数组?@Mr-K10他不是。该数组是他想从该对象提取的字段列表。@Amy是对的,如描述所述。您如何从JSON对象创建
字段
数组?@Mr-K10他不是。该数组是他想提取的字段列表。@Amy是对的。您如何从JSON对象创建
字段
数组?@Mr-K10他不是。该数组是他想提取的字段列表从对象开始行动。@Amy是对的,正如描述中所述。我喜欢你在这里使用reduce!:)我喜欢你在这里使用reduce!:)