簡體   English   中英

JavaScript:如果另一個對象在另一個屬性中具有相同的值,則更改對象的值

[英]JavaScript: Changing object value if another object has the same value in another property

我有一個帶有屬性名稱,首選項和表的對象數組。 我需要檢查一個對象是否具有與其他任何對象名稱值相同的首選項值。 我已經寫了一些代碼,但是似乎沒有用。

 function seat() { for (var i = 0; i < data.length; i++) { if (data[i].pref != "") { for (var c = 0; c < data.length; c++) { if (data[i].pref == data[c].name) { data[i].table = data[c].table console.log(data[i].table + "first pref val"); console.log(data[c].table + "second pref val"); } } } function randomize() { let counts = [ [1, 6], [2, 6], [3, 6], [4, 6] ]; data.forEach(obj => { let i = Math.floor(Math.random() * counts.length); obj.table = 'table' + counts[i][0]; if (--counts[i][1] == 0) counts.splice(i, 1); }) } randomize(1, 4); console.log(data); console.log("Right here ^"); }; }; var data = [{ name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, { name: "", pref: "", table: "" }, ]; seat(); 

您可以將朋友分配到同一組:如果任何人的首選人在現有組中,則將他們添加到同一組中,依此類推。這樣,您可以將所有人分為不同的組,以確保沒有鏈從一個組中的一個人到另一個組中的一個人的偏好。

然后,您可以按降序對這些組進行排序。 首先選擇最大的組,您可以將它們分配給仍然有足夠空間容納整個組的第一張桌子。

如果一個小組無法坐在任何桌子上,您應該放棄,因為這意味着(按照您的配置,有4個桌子和6個座位),沒有解決方案可以讓每個人都與他們的首選座位一起就座。

接下來是執行此操作的函數。 我添加了使代碼片段與輸入配合使用的代碼,但本質在於第一個功能:

 function seat(data) { // Key the persons by name and add some extra properties const hash = data.reduce( (acc, person, i) => acc.set(person.name, Object.assign(person, { id: i, group: null })), new Map ); const groups = []; const free = new Set(hash.values()); while (free.size) { const group = new Set(); let person = free.values().next().value; // first in Set // Add chain of preferrences to same group while (person && person.group === null) { free.delete(person); group.add(person); person.group = group; person = hash.get(person.pref); } if (person && person.group !== group) { // merge groups group.forEach( p => { p.group = person.group; p.group.add(p); }); } else { groups.push(group); // add group } } const counts = [6, 6, 6, 6]; groups.sort( (a, b) => b.size - a.size ) // descending size .forEach( group => { let table = counts.findIndex( count => count >= group.size ); if (table === -1) { alert('No solution possible'); return; } counts[table] -= group.size; // Assign table (table1, table2, table3 or table4) group.forEach( person => person.table = 'table' + (table + 1) ); }); } // Below follow the functions to make this demo work (function populate() { const persons = [...Array(6*4).keys()]; // template row: const $row = $('<tr>').append( $('<td>').append($('<input>')), $('<td>').append( $('<select>').addClass('pref') .append($('<option>'), persons.map(function (i) { return $('<option>').val(i+1).text('person' + (i+1)); })) ), $('<td>').append( $('<select>').addClass('table') .append($('<option>'), [1,2,3,4].map(function (i) { return $('<option>').val('table' + i).text('table' + i); })) ) ); // Fill table with names $('table').append( persons.map( i => { $tr = $row.clone(); $('input', $tr).val('person'+ (i+1)); // Remove option to select the same person as preferred $('.pref>option', $tr).get(i+1).remove(); return $tr; }) ); })(); // execute immediately function shuffle(a) { for (let i = a.length; i; i--) { let j = Math.floor(Math.random() * i); [a[i - 1], a[j]] = [a[j], a[i - 1]]; } } // Allow to assign "random" choices for the preferred persons $('#rand_pref').on('click', function () { const persons = [...Array(6*4).keys()]; shuffle(persons); $('tr').each(function (i) { // Select kind-of-random preferred compagnion, // but in a way that it is solvable const j = persons.indexOf(i); let k = ((j % 6)>>1 !== 1) ? j ^ 1 : j + Math.sign(Math.random() - 0.5); $('.pref', this).val(persons[k] + 1); }); }); // Allow names to be changed: $('input').on('input', function() { $('.pref>option[value=' + ($(this).closest('tr').index()+1) + ']') .text($(this).val()); }); // On click: collect input, and generate solution $('#assign').on('click', function() { // collect input var data = $('tr').map(function () { return { name: $('input', this).val(), pref: $('.pref>option:selected', this).text(), table: $('.table>option', this).val() }; }).get(); // Calculate seating seat(data); // Display result $('tr').each(function (i) { $('.table', this).val(data[i].table); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table></table> <button id="rand_pref">Randomise preferred persons</button> <button id="assign">Assign to tables</button> 

使用摘要的全頁模式查看整個表格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM