gpt4 book ai didi

javascript - 输出好友姓名而不是 ID

转载 作者:行者123 更新时间:2023-12-03 02:34:18 25 4
gpt4 key购买 nike

有人可以帮我解决这个问题吗,我是 javaScript 新手,现在被困住了。我在数组中输出了某个对象,该输出正在写入一个人的值,在该对象(Osoba)中有一个数组他的 friend ,里面的所有值都是每个人的 ID,http://prntscr.com/i9m2ti我怎样才能使 friend 数组(在对象内)的ID成为数组中那个人的名字和姓氏,当我想输出某个对象时,就会有 friend [他们的名字而不是ID ],有人可以写下我该怎么做吗?感谢您的理解。

class Osoba{
constructor(id,firstName,surname,age,gender,friends){
this._id = id ;
this._firstName = firstName;
this._surname = surname;
this._age = age;
this._gender = gender;
this._friends = friends;
}

get id() {
return this._id;
}
set id(id){
this._id = id;
}
get firstName() {
return this._firstName;
}
set firstName(firstName){
this._firstName = firstName;
}
get surname() {
return this._surname;
}
set surname(surname){
this._surname = surname;
}
get age() {
return this._age;
}
set age(age){
this._age = age;
}

get gender() {
return this._gender;
}
set gender(gender){
this._gender = gender;
}

get friends() {
return this._friends;
}
set friends(friends){
this._friends = friends;
}

}

var osobe = []; // my array


$(function() {

$.getJSON('https://raw.githubusercontent.com/Steffzz/damnz/master/
data.json' , function(data)
{
var json = jQuery.parseJSON(JSON.stringify(data));

for(person of json)
{

var id = person['id'] ;
var firstName = person['firstName'] ;
var surname = person['surname'] ;
var age = person['age'] ;
var gender= person['gender'] ;
var friends = person['friends'] ;

var x = new Osoba(id,firstName,surname,age,gender,friends);
osobe.push(x); //filling array with objects and their values
}
console.log(osobe);
document.write(JSON.stringify(osobe[0])) //output of a certain object
})
});

最佳答案

假设 json 是一个数组,包含所有可以映射到 Friends 数组的人,并找到具有该 id 的人: person['friends'].map(json.find(函数(person){person.id===friendId});

然后返回一个包含该人的名字和姓氏的对象:

console.log("json is:",JSON.stringify(json,undefined,3));
var friends = person['friends'].map(
function(friendId){
console.log("friendID is:",friendId);
var friend = json.find(function(person){return person.id===friendId;});
console.log("friend is:",JSON.stringify(friend,undefined,2));
return {
firstName:friend.firstName,
surname:friend.surname
}
}
);

现在,如果“不起作用”,您能否指定日志的输出、任何错误以及预期结果与实际结果?

更新完整代码

由于 json 是您的所有数据,因此您可以将其传递到 Osoba 构造函数中。 friend getter 将使用数据创建一个包含数据的 Osaba 项目数组,以及将创建 Osaba 数组的 friend ...

class Osoba {
constructor(id, firstName, surname, age, gender, friends, data) {//added data
this._id = id;
this._firstName = firstName;
this._surname = surname;
this._age = age;
this._gender = gender;
this._friends = friends;
this._data = data;//data has all the people
}

get id() {
return this._id;
}
set id(id) {
this._id = id;
}
get firstName() {
return this._firstName;
}
set firstName(firstName) {
this._firstName = firstName;
}
get surname() {
return this._surname;
}
set surname(surname) {
this._surname = surname;
}
get age() {
return this._age;
}
set age(age) {
this._age = age;
}

get gender() {
return this._gender;
}
set gender(gender) {
this._gender = gender;
}
//modified friends getter returning an array of Osoba items
get friends() {
var me = this;
return this._friends.map(
function (friendId) {
var friend = me._data.find(function (person) { return person.id === friendId; });
return new Osoba(
friend.id,
friend.firstName,
friend.surname,
friend.age,
friend.gender,
friend.friends,
me._data
);
}
);
}

set friends(friends) {
this._friends = friends;
}

}
$.getJSON('https://raw.githubusercontent.com/Steffzz/damnz/master/data.json')
.then(
json => {
var people = json.map(
person =>
new Osoba(
person.id,
person.firstName,
person.surname,
person.age,
person.gender,
person.friends,
json
)
);
//you can keep getting friends now, because Osoba is
// creating new Osoba objects based on id's and data you pass in
console.log(people[0].friends[0].friends[0].friends[0]);
}
);

关于javascript - 输出好友姓名而不是 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48599291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com