materials = {"Глина" : 10, "Цемент" : 5, "Вода" : 8, "Песок" : 14}
sort_arr = ["Красители", "Песок", "Цемент", "Вода", "Примеси"]
sort = function(object, pattern){
var result={}
pattern.forEach(function(name){
result[name]=object[name]
})
Object.keys(object).forEach(function(name){if(pattern.indexOf(name) === -1) result[name] = object[name]})
return result
}
materials=sort(materials, sort_arr)
alert(JSON.stringify(materials))
for (n in materials){
console.log(n+" "+materials[n])
}
//>>>> {"Песок":14,"Цемент":5,"Вода":8,"Глина":10}
|