从angularjs中的逗号分隔字符串列表中拆分和映射数组

时间:2015-12-23 21:30:17

标签: javascript angularjs dictionary angularjs-filter

我想合并两个以逗号分隔的列表CENTURY, BADGER& 65, 85形成一个类似:[{name: 'CENTURY', price: '65'}, {name: 'BADGER', price: 85}]的数组,列表位于json对象中:

{
    unit: '35 lb',
    brands: 'CENTURY, BADGER'
    prices: '65, 85'
}

所以我所做的是一个过滤器:

angular
    .module( 'app.purchases.products' )
    .filter( 'mergeDetails', mergeDetails );

function mergeDetails() {
    return function ( product ) {
        _.merge( product.brands, product.prices );//Using lodash, any suggestion?
        console.log('brands ', product.brands);
        return product.brands;//`_.merge` will add prices to brands
    }
}

我想知道如何将滤镜应用于插值{{ }},以便我可以获取数组并在ng-repeat中使用它,在这里使用它:

<tr ng-repeat="product in products">
    <td>{{product.unit}}</td>
    <td>
        <!-- Here I should filter to ng-repeat the resulting array -->
        {{product.brands +' '+ product.prices}}
    </td>
</tr>

5 个答案:

答案 0 :(得分:3)

我认为如果您要查找此cursor = db.test_collection.find({'i': {'$lt': 5}}).sort('i') for document in (yield cursor.to_list(length=100)): self.write(str(document)) 之类的输出,您只需要这样做

CENTURY, BADGER, EXO

我编辑这个添加过滤器可能有所帮助 添加此过滤器

  <li ng-repeat="p in products">
    <p>{{p.brands}}</p>
  </li>

您的HTML视图已修改为

 .filter('customSplit', function() {
  return function(input) {
    console.log(input);
    var ar = input.split(','); // this will make string an array 
    return ar;
  };
});

答案 1 :(得分:1)

Array.map在转换数组时非常有用。

如果你有一个起始“产品”,你可以把它变成这样的数组:

self.variable_evidence.arrays.append(lambda: self.basic_in)

你可以在此基础上改造它们的数组。

答案 2 :(得分:0)

这应该这样做:

var transpose = function(obj){
    var result =[];
    for(key in obj){
        var aux = obj[key].split(",");
        for(i in aux){
            if(result[i]==null){
                result[i]={};
            }
            result[i][key]=aux[i].trim()
        }
    }
    return result
}

答案 3 :(得分:0)

就个人而言,我不喜欢过滤器作为此问题的解决方案,因为正如术语定义的那样,它应该用于过滤/选择要显示的项目。正如Steve Campbell所示,您应该将产品转换/映射到您想要的形式。 Demo

// flatten the pr0ducts into one array ...
vm._products = data.reduce(function (previous, current) {
                        return previous.concat(generateMergeList(current))
                    }, []);

// helper function to generate a list of product in a product data object
function generateMergeList(source) {
var result = [];
if (!source || !source.brands || !source.prices) {
    return result;
}

var _brands = source.brands.split(',');
var _prices = source.prices.split(',');

if (_brands.length !== _prices.length) {
    throw 'Number of items in brands and prices aren\'t the same...';
}

return _brands.map(function (elm, idx) {
    return {
        brand : elm,
        price : _prices[idx],
        unit : source.unit
    };
});
}

答案 4 :(得分:0)

let arrayDefaultDeliveryDay = Array.from(this.defaultDeliveryDays.split(","));
let arrayDefaultDeliveryFoodType = Array.from(this.defaultDeliveryFoodType.split(","));
let arrayDefaultDeliveryStartTime = Array.from(this.defaultDeliveryStartTime.split(","));
let arrayDefaultDeliveryEndTime = Array.from(this.defaultDeliveryEndTime.split(","));

var deliveryDetails = [];

for(var i = 0; i<=arrayDefaultDeliveryDay.length-1; i++){
  deliveryDetails.push({
    foodType : arrayDefaultDeliveryFoodType[i],
    day : arrayDefaultDeliveryDay[i],
    startTime : arrayDefaultDeliveryStartTime[i],
    endTime : arrayDefaultDeliveryEndTime[i]
  })
}

console.log(JSON.stringify(deliveryDetails));

经过大量研究,终于找到了解决方法。