Добрый вече форумчане, помогите правильно перебрать объект.
Имеем объект с вложенными объектами. И структуру инпутов. Нужно перебором разместить в value инпутов значения объектов.
В примере показал 3 типа клиентов, данные в объекте: "min кол-во": "цена"
Ссылка на фидл
<div id="extra_prices2" class="container alert">
<div id="price_to_all" class="panel col-xs-12" aria-labelledby="price_to_all-tab">
<div class="form-group">
<div class="col-xs-4">
<input type="text" class="form-control impact_extra_value" value="">
</div>
<div class="col-xs-offset-1 col-xs-4">
<div class="input-group"> <span class="input-group-addon"><span class="initial">min</span></span>
<input type="text" class="form-control impact_initial" value="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-4">
<input type="text" class="form-control impact_extra_value" value="">
</div>
<div class="col-xs-offset-1 col-xs-4">
<div class="input-group"> <span class="input-group-addon"><span class="initial">min</span></span>
<input type="text" class="form-control impact_initial" value="">
</div>
</div>
</div>
</div>
<div id="price_to_client1" class="panel col-xs-12" aria-labelledby="price_to_all-tab">
<div class="form-group">
<div class="col-xs-4">
<input type="text" class="form-control impact_extra_value" value="">
</div>
<div class="col-xs-offset-1 col-xs-4">
<div class="input-group"> <span class="input-group-addon"><span class="initial">min</span></span>
<input type="text" class="form-control impact_initial" value="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-4">
<input type="text" class="form-control impact_extra_value" value="">
</div>
<div class="col-xs-offset-1 col-xs-4">
<div class="input-group"> <span class="input-group-addon"><span class="initial">min</span></span>
<input type="text" class="form-control impact_initial" value="">
</div>
</div>
</div>
</div>
<div id="price_to_client2" class="panel col-xs-12" aria-labelledby="price_to_all-tab">
<div class="form-group">
<div class="col-xs-4">
<input type="text" class="form-control impact_extra_value" value="">
</div>
<div class="col-xs-offset-1 col-xs-4">
<div class="input-group"> <span class="input-group-addon"><span class="initial">min</span></span>
<input type="text" class="form-control impact_initial" value="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-4">
<input type="text" class="form-control impact_extra_value" value="">
</div>
<div class="col-xs-offset-1 col-xs-4">
<div class="input-group"> <span class="input-group-addon"><span class="initial">min</span></span>
<input type="text" class="form-control impact_initial" value="">
</div>
</div>
</div>
</div>
</div>
var extra_values_group = '{"price_to_all":{"100":"1.546","200":"2.227"},"price_to_client1":{"100":"1.007","200":"0.124"},"price_to_client2":{"100":"1.007","200":"0.124"}}',
extra_values = JSON.parse(extra_values_group, function (k, v) {
return v;
}),
new_parent = $('#extra_prices2'),
group_all = '#price_to_all',
group_client1 = '#price_to_client1',
group_client2 = '#price_to_client2',
impact_initial = '.impact_initial',
impact_extra_value = '.impact_extra_value',
all_values = extra_values.price_to_all,
all_initials = Object.keys(all_values),
client1_values = extra_values.price_to_client1,
client2_values = extra_values.price_to_client2;
console.log(extra_values);
console.log(all_values);
console.log(all_initials);
extravaluestoinput(group_all);
function extravaluestoinput(parent) {
new_parent.find(parent).find(impact_extra_value).each(function() {
for (var i in all_values) {
$(this).val(all_values[i]);
};
});
new_parent.find(parent).find(impact_initial).each(function() {
for (var i in all_initials) {
$(this).val(all_initials[i]);
};
});
}