te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>arrays - Return subset of JSON Object using Javascript map() function - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Return subset of JSON Object using Javascript map() function - Stack Overflow

programmeradmin4浏览0评论

My question is if there is a simple way to return a subset of JSON object that will contain all 'columns' rather than specifying individually which 'columns' to return.

In particular, I have a multi-column csv file converted to JSON. The function below returns a subset of that object - just two 'columns' (as another object) if certain condition is met (in this case 'Name' is matched):

var firstSubArr = json.map(function(s) {
  if (s.Name === RegName) {
    return {
      'Price': s.Price,
      'Name': s.Name
    }
  }
}).filter(function(n) {
  return n !== undefined
});

Is there a simpler way to return "all columns" from json object as object rather that this:

return {'Price':s.Price,'Name':s.Name}?

Comment: It is just a simple JSON structure after conversion from csv, eg:

`[
    {Name:'Sydney', Price:123, Type:'xyz', etc. 20 more... },
    etc.
    ]`

Comment 2: Yes filter may be an option as a couple of people suggested but what if my condition is a bit more plex, like:

    var fullPeriodArr= json.map( function (s) { 
    if(moment(s.Date,"YYYYMMDD").isSameOrBefore(currd) && moment(s.Date,"YYYYMMDD").isSameOrAfter(fullPeriodStart) && (s.Type==sid || s.Type==sid2)&& s.Name===RegName){return [s.Price, s.Name] 
    }
    }).filter( function (n) { 
    return n!== undefined
    });

SOLUTION:

All 3 respondents provided the clue, thanks! It was just “return every object in the array of objects that matches the condition”, or simply:

    var firstSubArr= json.filter( 

s =>  (moment(s.Date,"YYYYMMDD").isSameOrBefore(currd) && moment(s.Date,"YYYYMMDD").isSameOrAfter(fullPeriodStart) && (s.Type==sid  || s.Type==sid2) && s.Name===RegName)

);

. where time range 'from... to' is evaluated using moment.js lib and Date, Type and Name are object keys.

The secret pudding was the round bracket ( ) around the plex condition.

How neat Javascript has bee: no need to get .length nor to loop with 'for' or 'while', no 'if... else' statements, or no need to store interim results, and no 'return'. The arrow replaces all that!

Then you can access eg. Price 'column' as array of numbers for summary calculations (eg. sum of values):


var firstSubArrPrice=firstSubArr.map(t => t.Price );

My question is if there is a simple way to return a subset of JSON object that will contain all 'columns' rather than specifying individually which 'columns' to return.

In particular, I have a multi-column csv file converted to JSON. The function below returns a subset of that object - just two 'columns' (as another object) if certain condition is met (in this case 'Name' is matched):

var firstSubArr = json.map(function(s) {
  if (s.Name === RegName) {
    return {
      'Price': s.Price,
      'Name': s.Name
    }
  }
}).filter(function(n) {
  return n !== undefined
});

Is there a simpler way to return "all columns" from json object as object rather that this:

return {'Price':s.Price,'Name':s.Name}?

Comment: It is just a simple JSON structure after conversion from csv, eg:

`[
    {Name:'Sydney', Price:123, Type:'xyz', etc. 20 more... },
    etc.
    ]`

Comment 2: Yes filter may be an option as a couple of people suggested but what if my condition is a bit more plex, like:

    var fullPeriodArr= json.map( function (s) { 
    if(moment(s.Date,"YYYYMMDD").isSameOrBefore(currd) && moment(s.Date,"YYYYMMDD").isSameOrAfter(fullPeriodStart) && (s.Type==sid || s.Type==sid2)&& s.Name===RegName){return [s.Price, s.Name] 
    }
    }).filter( function (n) { 
    return n!== undefined
    });

SOLUTION:

All 3 respondents provided the clue, thanks! It was just “return every object in the array of objects that matches the condition”, or simply:

    var firstSubArr= json.filter( 

s =>  (moment(s.Date,"YYYYMMDD").isSameOrBefore(currd) && moment(s.Date,"YYYYMMDD").isSameOrAfter(fullPeriodStart) && (s.Type==sid  || s.Type==sid2) && s.Name===RegName)

);

. where time range 'from... to' is evaluated using moment.js lib and Date, Type and Name are object keys.

The secret pudding was the round bracket ( ) around the plex condition.

How neat Javascript has bee: no need to get .length nor to loop with 'for' or 'while', no 'if... else' statements, or no need to store interim results, and no 'return'. The arrow replaces all that!

Then you can access eg. Price 'column' as array of numbers for summary calculations (eg. sum of values):


var firstSubArrPrice=firstSubArr.map(t => t.Price );

Share Improve this question edited Apr 11, 2019 at 11:48 edaus asked Apr 10, 2019 at 12:56 edausedaus 1991 gold badge1 silver badge13 bronze badges 2
  • Can you please elaborate a bit more and add tell how json looks like. – Maheer Ali Commented Apr 10, 2019 at 12:59
  • Do you want it to be just pure js? – lankovova Commented Apr 10, 2019 at 13:01
Add a ment  | 

4 Answers 4

Reset to default 8

Get rid of the map() and just use filter() and you will have the original objects in resultant

var firstSubArr = json.filter(function(s) {
  return s.Name === RegName
});

I understood that you want to return the the object with specific properties which matches the condition.You can do that using reduce()

var firstSubArr = json.reduce((ac,{Name,Price}) => a.Name === RegName ? [...ac,{Name,Price}] : ac, []);

If you don't want only Name,Price and all properties with calling names. Then use filter() alone

var firstSubArr = json.filter(x => x.Name === RegName)

How about:

var firstSubArr = json.filter( x => x.Name ===RegName)  

To make it in JavaScript I remend this code:

var valueNeedly = json.filter(function(s) {
  return s.value === searchValue
});
发布评论

评论列表(0)

  1. 暂无评论