Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用js对象值_Javascript_Arrays_Object - Fatal编程技术网

Javascript 使用js对象值

Javascript 使用js对象值,javascript,arrays,object,Javascript,Arrays,Object,嗨,我有这个js对象,我想在数组中“保存”元素“price”,这样我就可以操纵它们了。我该怎么做 $(function(){ $("#elastic_grid_demo").elastic_grid({ 'filterEffect': 'scaleup', // moveup, scaleup, fallperspective, fly, flip, helix , popup 'hoverDirection': true, 'hoverD

嗨,我有这个js对象,我想在数组中“保存”元素“price”,这样我就可以操纵它们了。我该怎么做

$(function(){
    $("#elastic_grid_demo").elastic_grid({
        'filterEffect': 'scaleup', // moveup, scaleup, fallperspective, fly, flip, helix , popup
        'hoverDirection': true,
        'hoverDelay': 0,
        'hoverInverse': false,
        'expandingSpeed': 500,
        'expandingHeight': 500,
        'items' :
        [
            {
                'price'         : '10.95',
                'title'         : 'Azuki bean',
                'description'   : 'Swiss chard pumpkin bunya nuts maize plantain aubergine napa cabbage soko coriander sweet pepper water spinach winter purslane shallot tigernut lentil beetroot.Swiss chard pumpkin bunya nuts maize plantain aubergine napa cabbage.',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg', 'images/small/3.jpg', 'images/small/10.jpg', 'images/small/11.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg', 'images/large/3.jpg', 'images/large/10.jpg', 'images/large/11.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://porfolio.bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            }, /* ...more items*/
价格推送(10.95)


创建一个var price数组。然后可以使用数组方法“操纵”新数组中的元素

首先,必须将传递给弹性网格的对象分配到一个变量中:

var opts = {
  'filterEffect': 'scaleup',
   ... 
   ...
   items: [...],
   ...
};
然后,您可以只获取商品,并迭代构建一个新的价格数组:

var items = opts.items;
var prices = [];
for(var i = 0; i < items.length; i++) {
  prices.push(items[i].price);
}
// prices populated here
var items=opts.items;
var价格=[];
对于(变量i=0;i
使用
Array.map
(请参阅)从对象数组创建数组:

var prices = [yourArrayOfObjects].map(function(v) {return v.price;});

你的问题真的不清楚。是否希望at方法返回类似['10.95,…..]的数组?然后使用KooiInc的答案。使用Array.map。或者使用Hi,谢谢您的回答,但是如何在数组中保存对象值呢?Hi,这是可行的,但仅适用于第一个值prices[0],如果数据结构是您发布它的方式,则prices[]的其余值是“未定义”的,并且设置了“price”值。。。这肯定能用。好的,我写代码时出错了,现在可以了,谢谢。