Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【js技巧】异步得到的数据源,需要先进行缓存,并在必要时附加到original上 #49

Open
Kelichao opened this issue Jul 3, 2017 · 0 comments

Comments

@Kelichao
Copy link
Owner

Kelichao commented Jul 3, 2017

目前处理方式

// 异步得到的数据源,需要先进行缓存,在必要时附加到original上
var data = {
    productionQueues: [
        {
            addData: 123,
            otherData: 098
        },
        {
            addData: 456,
            otherData: 789
        }
    ]
};

// 为对象追加数据
var extend = [];

// 遍历data并在extend中进行缓存
kit.forEach(data.productionQueues, function(value, index) {
    extend.push(value.addData);
});

// [123, 456]

var original = {
    productionQueues: [
        {
            configEnums:[],
            productNum: "--",// 产品标识号
            productId: "--",// 生产管理号/产品管理号
            carDescription: "--",// 产品规格
        },
        {
            configEnums:[],
            productNum: "--",// 产品标识号
            productId: "--",// 生产管理号/产品管理号
            carDescription: "--",// 产品规格
        },
    ],
}

// 为original附加数据
kit.forEach(original.productionQueues, function(value, index) {
    // 赋值对象没有对应extend[index]看不到任何数据
    original.productionQueues[index].addData = extend[index];
})

改进处理方式

将单纯的数组存储方式改为用单一有效值数据对象进行存储

// 异步得到的数据源,需要先进行缓存,在必要时附加到original上
var data = {
    productionQueues: [
        {
            addData: 123,
            otherData: 098
        },
        {
            addData: 456,
            otherData: 789
        }
    ]
};

// 为对象追加数据
var extend = [];

// 遍历data并在extend中进行缓存
kit.forEach(data.productionQueues, function(value, index) {
    extend[index] = {
        addData: value.addData
    };
});

/*
    extend:[
        {
            addData:123
        },
        {
            addData:456
        }
    ]
*/

var original = {
    productionQueues: [
        {
            configEnums:[],
            productNum: "--",// 产品标识号
            productId: "--",// 生产管理号/产品管理号
            carDescription: "--",// 产品规格
        },
        {
            configEnums:[],
            productNum: "--",// 产品标识号
            productId: "--",// 生产管理号/产品管理号
            carDescription: "--",// 产品规格
        },
    ],
}

// 为original附加数据
kit.forEach(original.productionQueues, function(value, index) {
    // 在附加处不需要任何键值
    $.extend(true, original.productionQueues[index], extend[index]);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant