js数组工作异常结果

时间:2018-07-18 16:15:39

标签: javascript angularjs

我尝试从对象中删除排除的类型。并且认为应该起作用,但我不明白,为什么“ company_accountant”仍在数组中。 也许您可以帮助我,并为您提供更优雅的搜索和删除排除类型的方法?

html.Div([
    html.Div([html.H5(corner, style={'paddingLeft':'25px', 'color':'#fff'})], className='three columns summary'),
    html.Div([html.H5(int_status, style={'paddingLeft':'25px', 'color':'#fff'})], className='three columns'),
    html.Div([html.H5(ext_status, style={'paddingLeft':'25px', 'color':'#fff'})], className='three columns'),
    html.Div(html.A(children='\u2630', style={'paddingLeft':'60px', 'color':'#fff'}, href=f'http://10.38.234.26:5000/{partition}_timing_summary.xlsx'), style={'marginTop':'5px'}, className='one column offset-by-two')
], className='row'),

html.Div([
    html.Div([dcc.Graph(id=f'binning-{corner}', figure=create_timing_graphs(partition, corner), 
        config={'displayModeBar':False})], 
        style={'paddingLeft':'40px'})
], style={**card_colors['default']}, className='row detail'),

数据应为:

var data = [
    {id: 1, type: "exempt_dealer"},
    {id: 2, type: "licensed_dealer"},
    {id: 3, type: "partnership"},
    {id: 4, type: "company"},
    {id: 5, type: "licensed_dealer_accountant"},
    {id: 6, type: "company_accountant"},
    {id: 7, type: "partnership_accountant"}
];

var exclude_types = [
    'company_accountant',
    'licensed_dealer_accountant',
    'partnership_accountant'
];

angular.forEach(data, function (value, key) {
    if(exclude_types.includes(value.type)){
        data.splice(key, 1);
    }
});

但由于某些原因:

data = [
    {id: 1, type: "exempt_dealer"},
    {id: 2, type: "licensed_dealer"},
    {id: 3, type: "partnership"},
    {id: 4, type: "company"}
];

3 个答案:

答案 0 :(得分:2)

使用forEach方法,您一次可以处理一个数组。当您使用ID为5的商品时,您将其找到并删除,因此ID为6的商品将取代它。然后,ID为7的项将ID为6的项置于位置,因此这是检查的下一个项,而另一个项被跳过。改变正在循环的数组不是一个好主意。 使用array.filter方法更有意义:

var data = [
    {id: 1, type: "exempt_dealer"},
    {id: 2, type: "licensed_dealer"},
    {id: 3, type: "partnership"},
    {id: 4, type: "company"},
    {id: 5, type: "licensed_dealer_accountant"},
    {id: 6, type: "company_accountant"},
    {id: 7, type: "partnership_accountant"}
];

var exclude_types = [
    'company_accountant',
    'licensed_dealer_accountant',
    'partnership_accountant'
];

var result = data.filter(item => !exclude_types.includes(item.type));

console.log(result);

答案 1 :(得分:1)

splice在迭代过程中使数组变异。您应该使用Array.filter

var data = [{id: 1, type: "exempt_dealer"},{id: 2, type: "licensed_dealer"},{id: 3, type: "partnership"},{id: 4, type: "company"},{id: 5, type: "licensed_dealer_accountant"},{id: 6, type: "company_accountant"},{id: 7, type: "partnership_accountant"}];
var exclude_types =['company_accountant','licensed_dealer_accountant','partnership_accountant'];

data = data.filter(({type}) => !exclude_types.includes(type));
console.log(data);

答案 2 :(得分:0)

我认为,实现所需结果的最简单方法是使用Array.prototype.filter方法。在回调函数中,只需检查是否在type数组中找到当前元素的exclude_types属性:

var data = [{
    id: 1,
    type: "exempt_dealer"
  },
  {
    id: 2,
    type: "licensed_dealer"
  },
  {
    id: 3,
    type: "partnership"
  },
  {
    id: 4,
    type: "company"
  },
  {
    id: 5,
    type: "licensed_dealer_accountant"
  },
  {
    id: 6,
    type: "company_accountant"
  },
  {
    id: 7,
    type: "partnership_accountant"
  }
];

var exclude_types = [
  'company_accountant',
  'licensed_dealer_accountant',
  'partnership_accountant'
];

var filtered = data.filter(function(el) {
  return exclude_types.indexOf(el.type) === -1;
});

console.log(filtered);