简体   繁体   English

Javascript-将JSON对象属性值从数组转换为字符串

[英]Javascript - Convert JSON Object Property Value from Array to String

I have an issue where an API call I'm using is sending objects with one property that contains a single array value ( keys property in response below). 我使用的API调用发送的对象的一个​​属性包含单个数组值(以下响应中的keys属性),这是一个问题。 Unfortunately I cannot use this format as I must abide by Nested arrays in order to use the outputted values in a separate application like so [[value1, value2, value3, value4],[value1, value2, value3, value4]] . 不幸的是,我不能使用这种格式,因为必须遵守嵌套数组才能在单独的应用程序中使用输出的值,例如[[value1, value2, value3, value4],[value1, value2, value3, value4]] I plan on asking a separate question to tackle the nested array section unless someone thinks it is an easy fix (I believe I should use .map to convert the object). 我计划提出一个单独的问题来解决嵌套数组部分,除非有人认为这很容易解决(我相信我应该使用.map来转换对象)。

Here is the format of my objects (from console.log(searchQueries) ): 这是我的对象的格式(来自console.log(searchQueries) ):

[ { keys: [ 'hammer' ],
    clicks: 1369,
    impressions: 3151,
    ctr: 0.4344652491272612,
    position: 1.004443033957474 },
  { keys: [ 'woodmaking' ],
    clicks: 207,
    impressions: 6324,
    ctr: 0.03273244781783681,
    position: 4.35831752055661 },
  { keys: [ 'house trends' ],
    clicks: 1,
    impressions: 3,
    ctr: 0.3333333333333333,
    position: 4.666666666666666 },
  { keys: [ 'housing' ],
    clicks: 1,
    impressions: 36,
    ctr: 0.027777777777777776,
    position: 6.472222222222222 } ]
byProperty

Above response is passed from the following for-in loop the result of this API response array being nested in an object originally: 上面的响应是从以下for-in循环传递的,该API响应数组的结果最初嵌套在对象中:

for (var prop in res){
              searchQueries = res[prop];

              console.log(searchQueries);
}

Would the JSON.stringify method or .toString('keys') achieve what I'm looking for? JSON.stringify方法或.toString('keys')实现我想要的?

如果要将keys从数组转换为字符串,只需要遍历数组并进行更改:

searchQueries.forEach(function (obj) { obj.keys = obj.keys[0] })
 answer=Object.values(searchQueries).map(el=>{el.keys=el.keys[0];return Object.values(el)});
console.log(searchQueries);

https://jsbin.com/biyazunafu/1/edit?console https://jsbin.com/biyazunafu/1/edit?console

Loop over the main array, turn the Objects (el) keys array into a string, than turn the whole object into its values Array. 循环遍历主数组,将Objects(el)键数组转换为字符串,然后将整个对象转换为其值Array。 However, Object.values is experimental, so may dont use this on the users side, instead use this code transpiled to ES5 : 但是,Object.values是实验性的,因此请不要在用户端使用它,而应使用转译到ES5的代码:

answer=[];
for(key in searchQueries){
  answer.push(searchQueries[key]);
 }
answer=answer.map(function(el){
  el.keys=el.keys[0];
  var newel=[];
  for(key in el){
    newel.push(el[key]);
  }
 return newel;
});
  • 1st Get the keys values 1获取键值

     var keys = searchQueries.map(function(e){return e.keys;}); 

This will output : 这将输出:

[["hammer"], ["woodmaking"], ["house trends"], ["housing"]]
  • 2nd: Concat the resulting array 第二:连接结果数组

     var values = keys.join(',').split(','); 

The result : 结果 :

 ["hammer", "woodmaking", "house trends", "housing"]

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

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