2017-08-14 11 views
0

概要:新しい画像リンクを含む画像リンクを含む配列を更新する必要があります。同時に、以前にアップロードされたすべての画像を配列に保存しています。私の問題は、これを行う際に以前の画像リンクが結合されることです。以下の例。配列を修正するためにコードをどのように変更すればよいですか?助けてくれてありがとう。Javascript - 配列をループする

var allimages = [] 

    var allCurrentImages = req.body.oldimages 
    //this pulls all the previous image links 

     if (allCurrentImages && allCurrentImages.length > 2){ 
     for (i=0;i<allCurrentImages.length;i++){ 
     allimages.push(allCurrentImages[i]); 
     } 
    } 

    if (filepath && filepath.length > 2){ 
    allimages.push(filepath); 
    } 

PROBLEM

は、ここで問題です。 var allCurrentImagesに2つの画像がある場合、私はボディを要求しているので、配列はそれらを1つのアイテムにまとめます。

images[0] = uploads/598f4cc,uploads/53eew2w 
images[1] = uploads/7wusjw2w 

それはこのように見える必要があります:

images[0] = uploads/598f4cc 
images[1] = uploads/53eew2w 
images[2] = uploads/7wusjw2w 

だから私は何とかそれをプッシュする前に、別の部分にreq.body.oldimagesを分割する必要がある3枚の画像がある場合には、このようになります。配列。 (私は思う。)どんな助けや助言も大歓迎です!

+1

出力は単純に不可能ですか? 'allCurrentImages'が配列であり、2つのイメージを持つ場合、条件は実行される前に配列が3以上の長さを必要とするため、条件は実行されません。 – adeneo

+0

@adeneo私は間違っているかもしれませんが、私は.lengthが配列内のオブジェクトではないテキストの文字数をチェックしていると思っていました。 2枚の画像では、長さは48 – AndrewLeonardi

+0

@adeneoです。また、分割すると、以下のように分割されます。どんな考え? f、4、c、c、0,9,8,3、e、f、7,1,1,0、p、l、o、a、d、s、 a、d、2,7、e、e、6,1,5,0,2,7,4,8,3,5,9,9,9,2 – AndrewLeonardi

答えて

0

問題はMongooseモデルの "oldimages"は配列でしたが、EJSで印刷すると文字列として出力されました。 EJSを印刷しないようにしてこの問題を回避し、foundListings.currentimagesから配列を取り出しました。

ありがとうございました。

Listings.findById(req.params.id, function(err, foundListings){ 

    var allimages = [] 

    var allCurrentImages = foundListings.currentimages; 
    console.log('all images' + allCurrentImages) 


     if (allCurrentImages){ 
     for (i=0;i<allCurrentImages.length;i++){ 
     allimages.push(allCurrentImages[i]); 
     } 
    } 

    if (filepath && filepath.length > 2){ 
    allimages.push(filepath); 
    } 

}); 
0

変更し、この中に、単にあなたのコード、文字列のすべての文字をループし、それがさらに2つの以上のイメージのために働くべきであるにも取り除く:

allimages.concat(allCurrentImages.split(',')); 
+0

これはうまくいくはずですが、常に第2のイメージ上のサイトをフリーズさせます。私はスプリット( '、')が、私が賭けているこのような分割を引き起こしていることが重要な問題であることに気づいた:u、p、l、o、a、d、s、/、5,9,8、 f、4、c、c、0,9,8,3、e、f、7,1,1,0、a、d、2,7、e、e、6,1,5,0,2、 7,4,8,3,5,9,9,9,2 – AndrewLeonardi

+0

それ以外の場合は、すべての画像ではなく、文字列のすべての文字をループします。コード – quirimmo

1

あなたが前にそれを分割することができます。

var allCurrentImagesTmp = req.body.oldimages 
var allCurrentImages = []; 

for (i=0;i<allCurrentImagesTmp .length;i++){ 
    allCurrentImages.concat(allCurrentImagesTmp[i].split(",")); 
} 
... 
// your code 
1

req.body.oldimagesは文字列の配列ですか?それはあり得ることらしいから、それ以外の場合は

allimages.push(allCurrentImages[i].split(',')); 

:これまで

allimages.push(allCurrentImages[i]); 

:もしそうなら、あなたはこのことから、あなたのコードに行を変更することで、あなたが探しているものを達成することができるはずですあなたは、具体的カンマを探しているとあなたの利点にその情報を使用して、より正確な方法を試みることができる1つの長い文字列、:

var CurrentImages = allCurrentImages; // Use temp variable to protect original 
var CommaIndex = CurrentImages.indexOf(','); // Find index of first comma 
while (CommaIndex>0) { // If there is no comma present, indexOf returns -1 
    allimages.push(CurrentImages.substring(0, CommaIndex-1)); // Push the first image path to allimages 
    CurrentImages = CurrentImages.substring(CommaIndex+1, CurrentImages.length-1); // Grab the rest of the string after the first comma 
    CommaIndex = CurrentImages.indexOf(','); // Check for another comma 
} 
allimages.push(CurrentImages); // This pushes the final one after the last comma - or the only one if there was no comma. 
+0

を更新しました何らかの理由で、u、p、l、o、a、d、s、/、5,9,8、f、4、c、c、 e、f、7,1,1,0、a、d、2,7、e、e、6,1,5,0,2,7,4,8,3,5,9,9,9、 2 – AndrewLeonardi

+0

@AndrewLeonardi - req.body.oldimagesが文字列ではなく文字列であるかのように見えます。上記の私の改訂を見てください。 – LHM

1

ハムを私はあなたの目的については本当にわからない、私はそのいくつかの時間を理解します...あなたは、文字列&いつか配列を持って、あなたは別の配列の先頭に要素を追加したい私はこれを行うには正しい&適切な方法はな単純なものだと思う:

let allimages = [] 

let allCurrentImages = req.body.oldimages.split(','); 
//Split by coma 

allimages = allimages.concat(allCurrentImages); 
// attention contact return the concat array so you have to set it to a variable. 

このコードは動作するはずですが、場合にのみ、あなたがこれを制御したいならば、画像には "、"がありません。フロントエンドの正規表現でバックエンド&を防ぐ必要があります。

関連する問題