简体   繁体   English

从dojo / store获取列的不同值数组

[英]Get an array of distinct values for a column from dojo/store

My Scenario: 我的场景:

This is my test data in JSON Array format: 这是我的JSON数组格式的测试数据:

var testDataObj = [
  { col1: 'p0',   col2: 1, tbl: 'p',   col3: '14/01/2013', col4:'BOB'},
  { col1: 'p1',   col2: 2, tbl: 'p',   col3: '14/01/2013', col4:'SANDY'},
  { col1: 'p2',   col2: 3, tbl: 'p',   col3: '14/01/2013', col4:'JASON'},
  { col1: 'p3',   col2: 4, tbl: 'p',   col3: '14/01/2013', col4:'JASON'},
  { col1: 'p4',   col2: 5, tbl: 'p',   col3: '14/01/2013', col4:'SANDY'},
];

I created an object of dojo/store/Memory as following: 我创建了一个dojo / store / Memory对象,如下所示:

require(["dojo/store/Memory"], function(Memory){
  var gctsTestStore = new Memory({data: testDataObj});
});

What I want to achieve: 我想要实现的目标:

I want to get an array of distinct values of column col4 : 我想获得列col4的一系列不同值:

var distinctColumnValues=gctsTestStore.getDistinctValuesFromColumnName('col4');
//distinctColumnValues= ['BOB','SANDY','JASON']

My Request: 我的请求:

Currently, I am looping through JSON Array testDataObj to achieve the result, but I am exploring a more elegant solution using dojo/store's API. 目前,我正在循环使用JSON Array testDataObj来实现结果,但我正在使用dojo / store的API探索更优雅的解决方案。

I am using dojo/store/Memory here but I can also accept solution for other type of dojo/store. 我在这里使用dojo / store / Memory,但我也可以接受其他类型的dojo / store的解决方案。

Thanks. 谢谢。

There are no built in APIs for getting Distinct values from Memory Store. 没有用于从Memory Store获取Distinct值的内置API。 You need to write you own method in Javascript for getting the distinct. 你需要在Javascript中编写自己的方法来获得不同的。 Below is one way of writing it. 以下是编写它的一种方法。

var unique = {};
var distinct = [];
for( var i in array ){
 if( typeof(unique[array[i].col4]) == "undefined"){
  distinct.push(array[i].col4);
 }
 unique[array[i].col4] = 0;
}

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

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